c# - Providing a Generic to Mock -
i attempting generalize test code in solution mvc project. because we're writing same test each controller, thought use generics make 1 test needed.
unfortunately, i'm getting error: error 1 type 'trepo' must reference type in order use parameter 't' in generic type or method 'moq.mock<t>'
here's relevant code. can provide more if needed.
testbase.cs
public class testbase<tcontroller, tobject, trepo> tcontroller : irmccontroller<tobject> trepo : irmcrepository { public mock<trepo> repo { get; set; } }
companiestest.cs
public class companiestest : testbase<companiescontroller, company, icompanyrepository>
thanks help.
in moq, mock<t>
class has class
constraint on generic type. here's how it's defined:
public class mock<t> : mock t : class
so if intend use in class must define same class
constraint in addition irmcrepository
:
public class testbase<tcontroller, tobject, trepo> tcontroller : irmccontroller<tobject> trepo : class, irmcrepository { public mock<trepo> repo { get; set; } }
now code's gonna compile. that's basic generic constraints
in c#.
Comments
Post a Comment