nsubstitute

NSubstitute DbSet / IQueryable<T>

a 夏天 提交于 2019-11-27 11:28:07
So EntityFramework 6 is a lot better testable then previous versions. And there are some nice examples on the internet for frameworks like Moq, but the case is, I prefer using NSubstitute. I've got the "non-query" examples translated to work with the use of NSubstitute, but I can't get my head around the 'query-test'. How does Moq's items.As<IQueryable<T>>().Setup(m => m.Provider).Returns(data.Provider); translate to NSubstitute? I thought something like ((IQueryable<T>) items).Provider.Returns(data.Provider); but that didn't work. I've also tried items.AsQueryable().Provider.Returns(data

Returning the result of a method that returns another substitute throws an exception in NSubstitute

纵饮孤独 提交于 2019-11-27 06:44:00
问题 I have run into a weird issue while using NSubstitute a few times and although I know how to work around it I've never been able to explain it. I've crafted what appears to be the minimum required test to prove the problem and it appears to be something to do with using a method to create a substituted return value. public interface IMyObject { int Value { get; } } public interface IMyInterface { IMyObject MyProperty { get; } } [TestMethod] public void NSubstitute_ReturnsFromMethod_Test() {

Is it recommended to mock concrete class?

≡放荡痞女 提交于 2019-11-26 22:20:13
问题 Most of the examples given in mocking framework website is to mock Interface. Let say NSubstitute that I'm currently using, all their mocking examples is to mock interface. But in reality, I saw some developer mock concrete class instead. Is it recommended to mock concrete class? 回答1: In theory there is absolutely no problem mocking a concrete class; we are testing against a logical interface (rather than a keyword interface ), and it does not matter whether that logical interface is provided

NSubstitute - Testing for a specific linq expression

蓝咒 提交于 2019-11-26 14:36:45
问题 I am using the repository pattern in an MVC 3 application I am currently developing. My repository interface looks as follows: public interface IRepository<TEntity> where TEntity : IdEntity { void Add(TEntity entity); void Update(TEntity entity); void Remove(TEntity entity); TEntity GetById(int id); IList<TEntity> GetAll(); TEntity FindFirst(Expression<Func<TEntity, bool>> criteria); IList<TEntity> Find(Expression<Func<TEntity, bool>> criteria); } In a lot of instances, when coding methods in