I am trying to work around a mocking issue by creating a custom mock of IDbSet.
The custom mock:
public class DbSetMock : IDbSet<Tenant>
{
/* hidden all other implemented methods/properties */
public TDerivedEntity Create<TDerivedEntity>() where TDerivedEntity : class, Tenant
{
throw new NotImplementedException();
}
}
The create method gives a build error that I have no clue on how to solve:
cannot specify both a constraint class and the ‘class’ or ‘struct’ constraint
Simply removing class from the constraints results in another build error (which I also don’t understand 🙁 ).
The constraints for type parameter ‘TDerivedEntity’ of method ‘Tests.DAL.Tenants.DbSetMock.Create<TDerivedEntity>()’ must match the constraints for type parameter ‘TDerivedEntity’ of interface method ‘System.Data.Entity.IDbSet<BusinessLayer.DAL.Tenants.Tenant>.Create<TDerivedEntity>()’. Consider using an explicit interface implementation instead.
Can anybody help me successfully building this class?
Since the
TDerivedtype parameter is constrained to be aTenant, adding the constraintsclassorstructis redundant. Just remove theclassconstraint.UPDATE: Curiously there seems to be a conflict between the compiler errors here. If you “fix” one you get the other, in an infinite loop of despair. Luckily, the second error also gives us a way out: you can use an explicit interface implementation:
There seems to be no way to implement that method without using explicit interface implementation. If you need it as part of the public interface of the class, I suggest creating another method that the interface implementation forwards to: