Unit testing a method that uses (dynamic) using Moq - c#

I have the below method:
public void Enqueue(ICommand itemToQueue)
{
if (itemToQueue == null)
{
throw new ArgumentNullException("itemToQueue");
}
// Using the dynamic keywork to ensure the type passed in to the generic
// method is the implementation type; not the interface.
QueueStorage.AddToQueue((dynamic)itemToQueue);
}
With QueueStorage being a dependency that implements IQueueStorage. I wish to unit test it but the (dynamic) keyword seems to be blocking Moq from binding correctly to it. The keyword is used to correctly assign the concrete class type rather than ICommand interface type when it is added to the queue.
The unit test looks like this:
[Test]
public void Enqueue_ItemGiven_AddToQueueCalledOnQueueStorage()
{
int timesAddToQueueCalled = 0;
var dummyQueueStorage = new Mock<IQueueStorage>();
var testCommand = new TestCommand();
var queueManager = new AzureCommandQueueManager();
dummyQueueStorage
.Setup(x => x.AddToQueue(It.IsAny<TestCommand>()))
.Callback(() => timesAddToQueueCalled++);
queueManager.QueueStorage = dummyQueueStorage.Object;
queueManager.Enqueue(testCommand);
Assert.AreEqual(1, timesAddToQueueCalled);
}
Whilst test command is a blank implementation of ICommand:
private class TestCommand : ICommand
{
}
public interface ICommand
{
}
The timesAddedToQueuCalled is not being incremented. I've tried using It.IsAny<ICommand> and (testCommand) to no avail. It looks like the Callback method is not being executed. Can anyone see what I'm doing wrong?
EDIT: IQueueStorage code:
public interface IQueueStorage
{
void AddToQueue<T>(T item) where T : class;
T ReadFromQueue<T>() where T : class;
}

Here is code which works without problems:
public class AzureCommandQueueManager
{
public void Enqueue(ICommand itemToQueue)
{
if (itemToQueue == null)
throw new ArgumentNullException("itemToQueue");
QueueStorage.AddToQueue((dynamic)itemToQueue);
}
public IQueueStorage QueueStorage { get; set; }
}
public interface IQueueStorage
{
void AddToQueue<T>(T command) where T : class;
}
public class TestCommand : ICommand {}
public interface ICommand {}
And test method:
[Test]
public void Enqueue_ItemGiven_AddToQueueCalledOnQueueStorage()
{
int timesAddToQueueCalled = 0;
var dummyQueueStorage = new Mock<IQueueStorage>();
var testCommand = new TestCommand();
var queueManager = new AzureCommandQueueManager();
dummyQueueStorage
.Setup(x => x.AddToQueue(It.IsAny<TestCommand>()))
.Callback(() => timesAddToQueueCalled++);
queueManager.QueueStorage = dummyQueueStorage.Object;
queueManager.Enqueue(testCommand);
Assert.AreEqual(1, timesAddToQueueCalled);
}
The only difference I see - you have private modifier of TestCommand class. Btw if it is private, how do you access that class from your tests?

Related

Partial mock methods in a class using Autofixture, Moq and XUnit

I want to mock only some methods of a class and call the real implementation for other methods.
I have my sut class Test where the Runner class is injected in the constructor. This injected class has again a injected other class RunnerParam in the constructor.
The code is a simplified case of my real classes in trying to have only the basics.
[Fact]
public void Test()
{
var fixture = new Fixture().Customize(new AutoMoqCustomization());
var paramMock = fixture.Freeze<Mock<IRunnerParam>>();
paramMock.Setup(x => x.Multiplicator()).Returns(2);
var classMock = fixture.Freeze<Mock<IRunner>>();
classMock.Setup(x => x.Run()).Returns(5);
var test = fixture.Create<Test>();
var result = test.StartRunning(); // should be 5
var result2 = test.StartRunningImplementation(5); // should be 500
}
Supporting members
public interface IRunnerParam
{
int Multiplicator();
}
public class RunnerParam : IRunnerParam
{
public virtual int Multiplicator()
{
return 20;
}
}
public interface IRunner
{
int Run();
int RunImplementation(int param);
}
public class Runner : IRunner
{
protected virtual RunnerParam MultiParam { get; set; }
public Runner(RunnerParam multiParam)
{
MultiParam = multiParam;
}
public virtual int Run()
{
return 10;
}
public int RunImplementation(int param)
{
return 10 * MultiParam.Multiplicator() * param * Run();
}
}
public class Test
{
private readonly IRunner _runner;
public Test(IRunner runner)
{
_runner = runner;
}
public int StartRunning()
{
return _runner.Run();
}
public int StartRunningImplementation(int param)
{
return _runner.RunImplementation(param);
}
}
I want to mock and give a mocked value to the method Run in the class Runner, but to use the real implementation of the method RunImplementation.
I would expect to see for result2 500, but it's 0, meaning that the method is not seen as mocked up. In my eyes that is correct, but the Moq callbase is equal to true, so the real implementation should be taken, but it isn't.
What am I missing here?
In the shown simplified example, Test is only dependent on IRunner
private readonly IRunner _runner;
public Test(IRunner runner)
{
_runner = runner;
}
So that is all that needs to be mocked if the intention was to test Test class in isolation.
//...
var classMock = fixture.Freeze<Mock<IRunner>>();
classMock.Setup(x => x.Run()).Returns(5);
classMock.Setup(x => x.RunImplementation(It.IsAny<int>())).Returns(500);
//...
If Runner class is to be also tested in isolation, then a mocked RunnerParam would be needed to satisfy its dependencies.
It should however be dependent on the abstraction (interface) and not the concretion (implementation).
protected virtual IRunnerParam MultiParam { get; set; }
public Runner(IRunnerParam multiParam) {
MultiParam = multiParam;
}
This simplifies the isolated test as described in the original question
I want to mock and give a mocked value to the method Run in the class Runner, but to use the real implementation of the method RunImplementation.
//Arrange
var fixture = new Fixture().Customize(new AutoMoqCustomization());
var runnerParam = fixture.Freeze<Mock<IRunnerParam>>()
.Setup(_ => _.Multiplicator())
.Returns(2);
var subjectMock = fixture.Freeze<Mock<Runner>>();
subjectMock.CallBase = true;
subjectMock.Setup(_ => _.Run()).Returns(5);
int expected = 500;
Runner sut = subjectMock.Object;
//Act
var actual = sut.RunImplementation(5); // should be 500
//Assert
actual.Should().Be(expected);

AutoFixture AutoMoq not use my mocks for properties

I have an abstract class that I want to test. There is an abstract property in this class for my DAO, which I define in inherited classes.
public abstract class DeviceGroupManagerBase<TDeviceGroup> where TDeviceGroup : DeviceGroup
{
protected abstract IDeviceGroupDao<TDeviceGroup> DeviceGroupDao { get; }
public TDeviceGroup UpdateDeviceIndexes(Device device)
{
return DeviceGroupDao.GetDeviceGroup(device.Group.Id);
}
}
I want to test the Updatedeviceindexes method so I'm trying to mock a property called DeviceGroupDao.
[TestFixture]
[Category("Unit")]
public class DeviceGroupManagerBaseTests
{
private IFixture fixture;
private Mock<DeviceGroupManagerBase<DeviceGroup>> subject;
private Mock<IDeviceGroupDao<DeviceGroup>> deviceGroupDaoMock;
private DeviceGroupManagerBase<DeviceGroup> Manager => subject.Object;
[TestFixtureSetUp]
public void Init()
{
fixture = new Fixture().Customize(new AutoMoqCustomization());
deviceGroupDaoMock = fixture.Freeze<Mock<IDeviceGroupDao<DeviceGroup>>>();
subject = fixture.Freeze<Mock<DeviceGroupManagerBase<DeviceGroup>>>();
}
[Test]
public void TestUpdateDeviceIndexes()
{
var device = fixture.Create<Device>();
var deviceGroup = fixture.Create<DeviceGroup>();
deviceGroupDaoMock.Setup(x => x.GetDeviceGroup(It.IsAny<int>())).Returns(deviceGroup);
var result = Manager.UpdateDeviceIndexes(device);
// The resultDeviceGroup will not be contain a previously defined object
Assert.AreEqual(deviceGroup.Id, result.Id);
}
}
I also tried to add registration for my device object in this way:
fixture.Register(() => deviceGroup);
But I'm still getting a new object.
How can I mock IDeviceGroupDao<TDeviceGroup>?
Since DeviceGroupManagerBase is an abstract base class, you'll need a SUT Double. It's easiest to do if you make the DeviceGroupDao property public:
public abstract class DeviceGroupManagerBase<TDeviceGroup> where TDeviceGroup : DeviceGroup
{
public abstract IDeviceGroupDao<TDeviceGroup> DeviceGroupDao { get; }
public TDeviceGroup UpdateDeviceIndexes(Device device)
{
return DeviceGroupDao.GetDeviceGroup(device.Group.Id);
}
}
Otherwise, you'll need to use Moq's API for defining and overriding protected members, which is possible, but more work.
Then you'll need to override subject's DeviceGroupDao property:
subject.SetupGet(x => x.DeviceGroupDao).Returns(deviceGroupDaoMock.Object);
Here's the full test:
[TestFixture]
[Category("Unit")]
public class DeviceGroupManagerBaseTests
{
private IFixture fixture;
private Mock<DeviceGroupManagerBase<DeviceGroup>> subject;
private Mock<IDeviceGroupDao<DeviceGroup>> deviceGroupDaoMock;
private DeviceGroupManagerBase<DeviceGroup> Manager => subject.Object;
[OneTimeSetUp]
public void Init()
{
fixture = new Fixture().Customize(new AutoMoqCustomization());
deviceGroupDaoMock = fixture.Freeze<Mock<IDeviceGroupDao<DeviceGroup>>>();
subject = fixture.Freeze<Mock<DeviceGroupManagerBase<DeviceGroup>>>();
subject.SetupGet(x => x.DeviceGroupDao).Returns(deviceGroupDaoMock.Object);
}
[Test]
public void TestUpdateDeviceIndexes()
{
var device = fixture.Create<Device>();
var deviceGroup = fixture.Create<DeviceGroup>();
deviceGroupDaoMock.Setup(x => x.GetDeviceGroup(It.IsAny<int>())).Returns(deviceGroup);
var result = Manager.UpdateDeviceIndexes(device);
Assert.AreEqual(deviceGroup.Id, result.Id);
}
}
This now passes on my machine.
Because the DeviceGroupDao is protected you will need to find a way to access it externally. Create a stub that would allow you set it value.
public class DeviceGroupManagerBaseStub<TDeviceGroup>
: DeviceGroupManagerBase<TDeviceGroup> where TDeviceGroup : DeviceGroup {
private IDeviceGroupDao<TDeviceGroup> deviceGroupDao;
public DeviceGroupManagerBaseStub(IDeviceGroupDao<TDeviceGroup> deviceGroupDao) {
this.deviceGroupDao = deviceGroupDao;
}
protected override IDeviceGroupDao<TDeviceGroup> DeviceGroupDao {
get {
return deviceGroupDao;
}
}
}
You can then mock IDeviceGroupDao<TDeviceGroup> and inject it into the stub for your test.

Compare Action<T>

I have a service class as below:
public class MyService
{
private readonly IMyDependency _myDependency;
public MyService(IMyDependency myDependency)
{
_myDependency = myDependency;
}
public void MyHandler(string param)
{
// work
}
public void AnotherMethod()
{
_myDependency.DoWork(MyHandler);
}
}
How can I Unit Test that MyHandler has been given as a parameter of DoWork()?
Since you are using Moq, you can write test like this:
[TestMethod]
public void DoWorkWasCalledWithCorrectParameters()
{
var mock = new Moq.Mock<IMyDependency>();
var myService = new MyService(mock.Object);
myService.AnotherMethod();
// verify that method was called once and with correct parameter:
mock.Verify(x => x.DoWork(myService.MyHandler), Moq.Times.Once);
}

Mock certain part of the method using Moq

I'm new to Moq and I would like to mock certain part of my method to test the business logic but having problem to mock the GetCountry method. Below is the code that I used as sample.
public class Class1
{
public void Process()
{
MyClass foo = new MyClass();
var o = foo.GetCountry(); //I would like to mock this part.
//Business Logic here
}
}
public class MyClass : IFoo
{
public List<string> GetCountry()
{
//Get the data from Database.. someone will do this
throw new NotImplementedException();
}
}
Below is my Test Code that I used.
[TestMethod]
public void TestMethod2()
{
var mock = new Moq.Mock<IFoo>();
mock.Setup(m => m.GetCountry()).Returns(new List<string> { "America", "Philippines", "Japan" });
ClassLibrary1.Class1 foo = new ClassLibrary1.Class1();
//still called the not implemented exception
foo.Process();
}
Your code currently doesn't have an easy way to replace one implementation to another. Try this approach:
public class Class1
{
// Instead of using a concrete class, use an interface
// also, promote it to field
IFoo _foo;
// Create a constructor that accepts the interface
public Class1(IFoo foo)
{
_foo = foo;
}
// alternatively use constructor which provides a default implementation
public Class1() : this(new MyClass())
{
}
public void Process()
{
// Don't initialize foo variable here
var o = _foo.GetCountry();
//Business Logic here
}
}
If you have such setup it is quite easy to mock it using your code:
[TestMethod]
public void TestMethod2()
{
var mock = new Moq.Mock<IFoo>();
mock.Setup(m => m.GetCountry()).Returns(new List<string> { "America", "Philippines", "Japan" });
// Pass mocked object to your constructor:
ClassLibrary1.Class1 foo = new ClassLibrary1.Class1(mock.Object);
foo.Process();
}

Customization for creating implementations of a base type

So I have the following types:
public abstract class Base
{
public string Text { get; set; }
public abstract int Value { get; set; }
}
public class BaseImplA : Base
{
public override int Value { get; set; }
}
public class BaseImplB : Base
{
public override int Value
{
get { return 1; }
set { throw new NotImplementedException(); }
}
}
I want AutoFixture to alternate creating BaseImplA and BaseImplB when Base is requested.
var fixture = new Fixture().Customize(new TestCustomization());
var b1 = fixture.Create<Base>();
var b2 = fixture.Create<Base>();
The issue is BaseImplB throws a NotImplementedException from the Value property setter. So I created the following customization:
public class TestCustomization : ICustomization
{
private bool _flag;
private IFixture _fixture;
public void Customize(IFixture fixture)
{
_fixture = fixture;
fixture.Customize<BaseImplB>(composer =>
{
return composer.Without(x => x.Value);
});
fixture.Customize<Base>(composer =>
{
return composer.FromFactory(CreateBase);
});
}
private Base CreateBase()
{
_flag = !_flag;
if (_flag)
{
return _fixture.Create<BaseImplA>();
}
return _fixture.Create<BaseImplB>();
}
}
But what's happening is that the Value is not being set for BaseImplA or BaseImplB. Can anyone point out where I'm going wrong?
With AutoFixture 3.18.5+, this isn't too difficult to do. There's at least two different issues in play here:
Dealing with BaseImplB
The BaseImplB class needs special treatment, which is quite easy to deal with. You only need to instruct AutoFixture to ignore the Value property:
public class BCustomization : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Customize<BaseImplB>(c => c.Without(x => x.Value));
}
}
This omits the Value property, but otherwise creates instances of BaseImplB as usual, including filling out any other writable properties, such as the Text property.
Alternating between different implementation
In order to alternate between BaseImplA and BaseImplB, you can write a Customization like this:
public class AlternatingCustomization : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Customizations.Add(new AlternatingBuilder());
}
private class AlternatingBuilder : ISpecimenBuilder
{
private bool createB;
public object Create(object request, ISpecimenContext context)
{
var t = request as Type;
if (t == null || t != typeof(Base))
return new NoSpecimen(request);
if (this.createB)
{
this.createB = false;
return context.Resolve(typeof(BaseImplB));
}
this.createB = true;
return context.Resolve(typeof(BaseImplA));
}
}
}
It simply deals with requests for Base, and relays alternating requests for BaseImplA and BaseImplB to the context.
Packaging
You can package up both Customizations (and others, if you have them) in a Composite, like this:
public class BaseCustomization : CompositeCustomization
{
public BaseCustomization()
: base(
new BCustomization(),
new AlternatingCustomization())
{
}
}
This will enable you to request BaseImplA, BaseImplB, and Base, as you need them; the following tests demonstrate this:
[Fact]
public void CreateImplA()
{
var fixture = new Fixture().Customize(new BaseCustomization());
var actual = fixture.Create<BaseImplA>();
Assert.NotEqual(default(string), actual.Text);
Assert.NotEqual(default(int), actual.Value);
}
[Fact]
public void CreateImplB()
{
var fixture = new Fixture().Customize(new BaseCustomization());
var actual = fixture.Create<BaseImplB>();
Assert.NotEqual(default(string), actual.Text);
Assert.Equal(1, actual.Value);
}
[Fact]
public void CreateBase()
{
var fixture = new Fixture().Customize(new BaseCustomization());
var actual = fixture.CreateMany<Base>(4).ToArray();
Assert.IsAssignableFrom<BaseImplA>(actual[0]);
Assert.NotEqual(default(string), actual[0].Text);
Assert.NotEqual(default(int), actual[0].Value);
Assert.IsAssignableFrom<BaseImplB>(actual[1]);
Assert.NotEqual(default(string), actual[1].Text);
Assert.Equal(1, actual[1].Value);
Assert.IsAssignableFrom<BaseImplA>(actual[2]);
Assert.NotEqual(default(string), actual[2].Text);
Assert.NotEqual(default(int), actual[2].Value);
Assert.IsAssignableFrom<BaseImplB>(actual[3]);
Assert.NotEqual(default(string), actual[3].Text);
Assert.Equal(1, actual[3].Value);
}
A note on versioning
This question surfaced a bug in AutoFixture, so this answer will not work unmodified in versions of AutoFixture prior to AutoFixture 3.18.5.
A note on design
AutoFixture was originally build as a tool for Test-Driven Development (TDD), and TDD is all about feedback. In the spirit of GOOS, you should listen to your tests. If the tests are hard to write, you should consider your API design. AutoFixture tends to amplify that sort of feedback, and that also seems to be the case here.
As given in the OP, the design violates the Liskov Substitution Principle, so you should consider an alternative design where this is not the case. Such an alternative design is also likely to make the AutoFixture setup simpler, and easier to maintain.
Mark Seemann provided an excellent response. You can build a reusable rotating specimen builder for your abstract base types like this:
public class RotatingSpecimenBuilder<T> : ISpecimenBuilder
{
protected const int Seed = 812039;
protected readonly static Random Random = new Random(Seed);
private static readonly List<Type> s_allTypes = new List<Type>();
private readonly List<Type> m_derivedTypes = new List<Type>();
private readonly Type m_baseType = null;
static RotatingSpecimenBuilder()
{
s_allTypes.AddRange(AppDomain.CurrentDomain.GetAssemblies().SelectMany(s => s.GetTypes()));
}
public RotatingSpecimenBuilder()
{
m_baseType = typeof(T);
m_derivedTypes.AddRange(s_allTypes.Where(x => x != m_baseType && m_baseType.IsAssignableFrom(x)));
}
public object Create(object request, ISpecimenContext context)
{
var t = request as Type;
if (t == null || t != m_baseType || m_derivedTypes.Count == 0)
{
return new NoSpecimen(request);
}
var derivedType = m_derivedTypes[Random.Next(0, m_derivedTypes.Count - 1)];
return context.Resolve(derivedType);
}
}
Then register this specimen builder as your fixture customization for each base type like this:
var fixture = new Fixture.Customizations.Add(new RotatingSpecimenBuilder<YourBaseType>());

Categories