Setup method in Moq, ambiguous call

Try the generic version of Returns: var matchSetupRepository = new Mock<IMatchSetupRepository>(); matchSetupRepository .Setup(ms => ms.GetAll()) .Returns<IEnumerable<MatchSetup>>(null); or: var matchSetupRepository = new Mock<IMatchSetupRepository>(); matchSetupRepository .Setup(ms => ms.GetAll()) .Returns((IEnumerable<MatchSetup>)null); Instead. Because you’re passing the function null (and there are two overloads of Returns), the compiler does not know which overload you mean unless you cast the argument … Read more

Mock AsNoTracking Entity Framework

Looking at the source code of the AsNoTracking() extension method: public static IQueryable AsNoTracking(this IQueryable source) { var asDbQuery = source as DbQuery; return asDbQuery != null ? asDbQuery.AsNoTracking() : CommonAsNoTracking(source); } Since source (your DbSet<Product> you’re trying to mock) is indeed a DbQuery (because DbSet is deriving from DbQuery), it tries to invoke the … Read more

Using Moq to verify a parameter of type List?

You can get rid of the call to Setup and Verifiable on your mock. Just use Verify. I created a little test-project, and this worked for me: using System; using System.Collections.Generic; using System.Linq; using Moq; namespace csharp { public class SomeDTO { } public class SomeRepository { public virtual void Write(List<SomeDTO> list) { } } … Read more

Verifying event registration using Moq

The moq 4.13 introduced this feature. Now it is possible to verify if add\remove has been invoked. Therefore four new methods have been introduced: SetupAdd SetupRemove VerifyAdd VerifyRemove Example var mock = new Mock<IAdder<EventArgs>>(); mock.SetupAdd(m => m.Added += (sender, args) => { }); mock.Object.Added += (sender, args) => { }; mock.Object.Added += (sender, args) => … Read more

Returnsasync(null) throws a build error when using Moq for unit testing in VS15

There are two ReturnsAsync extension methods in Moq ReturnsExtensions class.They have following parameters: (this IReturns<TMock, Task<TResult>> mock, TResult value) (this IReturns<TMock, Task<TResult>> mock, Func<TResult> valueFunction) As you can see one accepts value which should be returned by task, and another accepts delegate which will return value. When you are passing null compiler don’t know whether … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)