Moq: Invalid callback. Setup on method with parameters cannot invoke callback with parameters

You need to call the generic overload of Callback with the specific types expected by the method. The following should work: sender.Setup(x => x.SendCommand(It.IsAny<MyCommand>(), false)) .Callback<ICommand, bool>((command, idFromContent) => { var myCommand = command as MyCommand; Assert.That(myCommand, Is.Not.Null); Assert.That(myCommand.Id, Is.EqualTo(cmd.Id)); Assert.That(myCommand.Name, Is.EqualTo(cmd.Name)); }) .Verifiable(); If the assertions fail in the callback then the test fails … Read more

Using Moq, how do I set up a method call with an input parameter as an object with expected property values?

You can use Verify. Examples: Verify that Add was never called with an UserMetaData with FirstName!= “FirstName1″: storageManager.Verify(e => e.Add(It.Is<UserMetaData>(d => d.FirstName!=”FirstName1”)), Times.Never()); Verify that Add was called at least once with an UserMetaData with FirstName== “FirstName1″: storageManager.Verify(e => e.Add(It.Is<UserMetaData>(d => d.FirstName==”FirstName1”)), Times.AtLeastOnce()); Verify that Add was called exactly once with FirstName == “Firstname1” and … Read more

Returnsasync(null) creates 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

Mock IMemoryCache with Moq throwing exception

According to source code for MemoryCacheExtensions.cs, The Get<TItem> extension method makes use of the following public static object Get(this IMemoryCache cache, object key) { cache.TryGetValue(key, out object value); return value; } public static TItem Get<TItem>(this IMemoryCache cache, object key) { return (TItem)(cache.Get(key) ?? default(TItem)); } public static bool TryGetValue<TItem>(this IMemoryCache cache, object key, out TItem … Read more

Moq and throwing a SqlException

If you need test cases for the Number or Message properties of the exception, you could use a builder (which uses reflection) like this: using System; using System.Data.SqlClient; // .NetCore using Microsoft.Data.SqlClient; using System.Linq; using System.Reflection; public class SqlExceptionBuilder { private int errorNumber; private string errorMessage; public SqlException Build() { SqlError error = this.CreateError(); SqlErrorCollection … Read more

Mocking EF core dbcontext and dbset

I see you are using EF core DbContext in your MovieRepository. So instead of using mock, Using EF Core InMemory database will be a great option for you. This will also reduce the complexity. Write your GetAllTest() method as follows: [Fact] public void GetAllTest() { var options = new DbContextOptionsBuilder<MovieDbContext>() .UseInMemoryDatabase(databaseName: “MovieListDatabase”) .Options; // Insert … Read more

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