Category Archives: Testing

Comparing Moq and JustMock lite

This is not meant as a “which is best” post or even a feature blow by blow comparison but more a “I’m using JustMock lite (known henceforth as JML) how do I do this in Moq” or vice versa.

Please note, MOQ version 4.20 has introduced a SponsoreLink which appears to send data to some third party. See discussions on GitHub.

For this post I’m using Moq 4.2.1402.2112 and JML 20014.1.1424.1

For the code samples, I’m writing xUnit tests but I’m not necessarily going to write code to use the mocks but will instead call directly on the mocks to demonstrate solely how they would work. Such tests would obviously only really ensure the mocking framework worked as expected, but hopefully the ideas of the mocks usage is conveyed in as little code as possible.

Strict behaviour

By default both Moq and JML use loose behavior meaning simply that if we do not create any Setup or Arrange code for methods/properties being mocked, then the mocking framework will default them. When using strict behavior we are basically saying if a method or property is called on the mock object and we’ve not setup any behaviors for the mock object, then the mocking framework should fail – meaning we’ll get an exception from the mocking framework.

Following is an example of using the strict behavior – removing the Setup/Arrange will cause a mocking framework exception, adding the Setup/Arrange will fulfill the strict behavior and allow the code to complete

Using Moq

Mock<IFeed> feed = new Mock<IFeed>(MockBehavior.Strict);

feed.Setup(f => f.GetTitle()).Returns("");

feed.Object.GetTitle();

Using JML

IFeed feed = Mock.Create<IFeed>(Behavior.Strict);

Mock.Arrange(() => feed.GetTitle()).Returns("");

feed.GetTitle();

Removing the MockBehavior.Strict/Behavior.Strict from the mock calls will switch to loose behaviors.

Ensuring a mocked method/property is called n times

Occasionally we want to ensure that a method/property is called a specified number of times only, for example, once, at least n times, at most n etc.

Using Moq

Mock<IFeed> feed = new Mock<IFeed>();

feed.Setup(f => f.GetTitle()).Returns("");

feed.Object.GetTitle();

feed.Verify(f => f.GetTitle(), Times.Once);

Using JML

IFeed feed = Mock.Create<IFeed>();

Mock.Arrange(() => feed.GetTitle()).Returns("").OccursOnce();

feed.GetTitle();

Mock.Assert(feed);

In both examples we could change OccursOnce()/Times.Once to OccursNever()/Times.Never or Occurs(2)/Times.Exactly(2) and so on.

Throwing exceptions

On occasion we may want to mock an exception, maybe our IFeed throws a WebException if it cannot download data from a website, we want to simulate this on our mock object -then we can use the following

Using Moq

Mock<IFeed> feed = new Mock<IFeed>();

feed.Setup(f => f.Download()).Throws<WebException>();

Assert.Throws<WebException>(() => feed.Object.Download());

feed.Verify();

Using JML

IFeed feed = Mock.Create<IFeed>();

Mock.Arrange(() => feed.Download()).Throws<WebException>();

Assert.Throws<WebException>(() => feed.Download());

Mock.Assert(feed);

Supporting multiple interfaces

Occasionally we might be mocking an interface, such as IFeed but our application will check if the IFeed object also supports IDataErrorInfo (for example) and handle the code accordingly. So, without actually changing the IFeed what we would expect is a concrete class which implements both interfaces.

Using Moq

Mock<IFeed> feed = new Mock<IFeed>();
feed.As<IDataErrorInfo>();

Assert.IsAssignableFrom(typeof(IDataErrorInfo), feed.Object);

Using JML

IFeed feed = Mock.Create<IFeed>(r => r.Implements<IDataErrorInfo>());

Assert.IsAssignableFrom(typeof(IDataErrorInfo), feed);

As you can see, we add interfaces to our mock in Moq by using the As method and in JML using the Implements method, we can change these methods together to also add further interfaces to our mock as per

Using Moq

Mock<IFeed> feed = new Mock<IFeed>();
feed.As<IDataErrorInfo>().
     As<INotifyPropertyChanged>();

Assert.IsAssignableFrom(typeof(IDataErrorInfo), feed.Object);
Assert.IsAssignableFrom(typeof(INotifyPropertyChanged), feed.Object);

Using JML

IFeed feed = Mock.Create<IFeed>(r => 
   r.Implements<IDataErrorInfo>().
     Implements<INotifyPropertyChanged>());

Assert.IsAssignableFrom(typeof(IDataErrorInfo), feed);
Assert.IsAssignableFrom(typeof(INotifyPropertyChanged), feed);

Automocking

One of the biggest problems when unit testing using mocks is when a system under test (SUT) requires many parts to be mocked and setup, or if the code for the SUT changes often, requiring refactoring of tests to simply add/change etc. the mock objects used.

As you’ve already seen with Loose behavior we can get around the need to setup every single bit of code and thus concentrate our tests on specific areas without creating a thousand and one mock’s and setup/arrange sections of code. But in a possibly ever changing SUT it would be good if we didn’t need to continually add/remove mocks which we might not be testing against.

What would be nice is if the mocking framework could work like an IoC system and automatically inject the mocks for us – this is basically what auto mocking is about.

So if we look at the code below, assume for a moment that initially the code didn’t include IProxySettings, we write our IFeedList mock and write the code to test the RssReader, now we add a new interface IProxySettings and now we need to alter the tests to include this interface even though our current test code doesn’t need it. Ofcourse with the addition of a single interface this may seem to be a little over the top, however it can easily get a lot worse.

So here’s the code…

System under test and service code

public interface IFeedList
{
   string Download();
}

public interface IProxySettings
{		
}

public class RssReader
{
   private IFeedList feeds;
   private IProxySettings settings;

   public RssReader(IProxySettings settings, IFeedList feeds)
   {
      this.settings = settings;
      this.feeds = feeds;
   }

   public string Download()
   {
      return feeds.Download();
   }
}

Now when the auto mocking container mocks the RssReader, it will automatically inject mocks for the two interfaces, then it’s up to our test code to setup or arrange expectations etc. on it.

Using Moq

Unlike the code you will see (further below) for JML, Moq doesn’t come with a auto mock container by default (JML NuGet’s package will add the Telerik.JustMock.Container by default). Instead Moq appears to have several auto mocking containers created for use with it by the community at large. I’m going to concentrate on Moq.Contrib which includes the AutoMockContainer class.

MockRepository repos = new MockRepository(MockBehavior.Loose);
AutoMockContainer container = new AutoMockContainer(repos);

RssReader rss = container.Create<RssReader>();

container.GetMock<IFeedList>().Setup(f => f.Download()).Returns("Data");

Assert.Equal("Data", rss.Download());

repos.VerifyAll();

Using JML

var container = new MockingContainer<RssReader>();

container.Arrange<IFeedList>(f => f.Download()).Returns("Data");

Assert.Equal("Data", container.Instance.Download());

container.AssertAll();

In both cases the auto mock container created our RssReader, mocking the interfaces passed to it.

That’s it for now, I’ll add further comparisons as and when I get time.

Step by step mocking with RhinoMock

Requirements

We’re going to work through a simple set of scenarios/tests using RhinoMocks and NUnit. So we’ll need the following to get started

I’m using the RhinoMocks NuGet package, version 3.6.1 for these samples and the NuGet package for NUnit 2.6.3.

What are we going to mock

For the sake of these examples we’re going to have an interface, which we will mock out, named ISession. Here’s the code for ISession

public interface ISession
{
   /// <summary>
   /// Subscribe to session events
   /// </summary>
   /// <param name="o">the object that recieves event notification</param>
   /// <returns>a token representing the subscription object</returns>
   object Subscribe(object o);
   /// <summary>
   /// Unsubscribe from session events
   /// </summary>
   /// <param name="token">a token supplied via the Subscribe method</param>
   void Unsubscribe(object token);
   /// <summary>
   /// Executes a command against the session object
   /// </summary>
   /// <param name="command">the command to be executed</param>
   /// <returns>an object representing any return value for the given command</returns>
   object Execute(string command);
}

Getting started

Before we can create any mock objects we need to create an instance of the factory class or more specifically the MockRepository. A common pattern within NUnit test fixtures is to create this during the SetUp, for example

[TestFixture]
public class DemoTests
{
   private MockRepository repository;

   [SetUp]
   public void SetUp()
   {
      repository = new MockRepository();
   }
}

The MockRepository is used to create mock objects but also can be used to record, replay and verify mock objects (and more). We’ll take a look at some of these methods as we go through this step by step guide.

If, as is often the cases with tests I’ve written with RhinoMock, we want to verify all expectations on our mocks. Using NUnit we can handle this in the TearDown, for example adding the following to the DemoTests class

[TearDown]
public void TearDown()
{
   repository.VerifyAll();
}

this will verify that all mock expectations have been met when the test fixture is torn down.

Mocks vs DynamicMocks

In previous posts on Moq and JustMock Lite I’ve mentioned strict and loose behavior. Basically loose behavior on a mock object means I do not need to supply expectations for every method call, property invocation etc. on a mocked object, whereas strict means the opposite, in that if we do not supply the expectation an ExpectationViolationExpectation will be thrown by RhinoMocks.

In RhinoMock terminology, mocks have strict semantics whereas dynamic mocks have loose.

So, to see this in action let’s add two tests to our test fixture

[Test]
public void TestMock()
{
   ISession session = repository.CreateMock<ISession>();
   repository.ReplayAll();

   Mapper mapper = new Mapper(session);
}

[Test]
public void TestDynamic()
{
   ISession session = repository.DynamicMock<ISession>();
   repository.ReplayAll();

   Mapper mapper = new Mapper(session);
}

Our Mapper class looks like the following

public class Mapper
{
   private readonly ISession session;
   private object token;

   public Mapper(ISession session)
   {
      this.session = session;

      token = session.Subscribe(this);
   }
}

don’t worry about repository.ReplayAll(), we’ll get to that in a minute or two.

Now if we run these two tests TestDynamic will succeed whereas TestMock will fail with an ExpectationViolationException. The dynamic mock worked because of the loose semantics which means it does not require all expectations set before usage. We can fix the TestMock method by writing an expectation for the call to the Subscribe method on the ISession interface.

So changing the test to look like the following

[Test]
public void TestMock()
{
   ISession session = repository.CreateMock<ISession>();
   Expect.Call(session.Subscribe(null)).IgnoreArguments().Return(null).Repeat.Any();

   repository.ReplayAll();

   Mapper mapper = new Mapper(session);
}

So in the above code we arrange our expectations. Basically we’re saying expect a call on the Subscribe method of the session mock object. In this case we pass in null and tell the mock to ignore the arguments, removing IgnoreArguments means we expect that Mapper will call the Subscribe method passing the exact arguments supplied in the expectation, i.e. in this case null.

Next we’re setting the expectation to return null and we don’t care how many times this method is called, so we call Repeat.Any(). If we wish to change the expectation to ensure the method is called just the once, we can change this to Repeat.Once() which is obviously more specific and useful for catching scenarios where a method is accidentally called more times than necessary. In our Mapper’s case this cannot happen and the method can only be called once, so we’d normally set this to Repeat.Once().

What we’ve done is supply defaults which is what the dynamic mock object would have probably implemented for our expectations as well. Hence why I used Repeat.Any() to begin with, so the implementation above will now cause the test to succeed.

Record/Playback

Now to return to repository.ReplayAll(). RhinoMocks works in a record/playback way, that is by default it’s in record mode so if in TestDynamic we comment out repository.ReplayAll() we’ll get the exception InvalidOperationException. The mock object is in a record state. We arrange our expectation in the record phase then act upon them during playback. As we are, by default, in record mode we can simply start creating our expectations, then when we’re ready to act on those mocked objects we switch the MockRepository to playback mode using repository.ReplayAll().

Arrange

As already mentioned we need to setup expectations on our mock object (unless we’re using dynamic mocks ofcourse). We do this during the arrange phase as was shown with the line

Expect.Call(session.Subscribe(null)).IgnoreArguments().Return(null).Repeat.Any();

One gotcha is if your method takes no arguments and returns void. So let’s assume ISession now has a method DoSomething which takes no arguments and returns void and see what happens…

Trying to write the following

Expect.Call(session.DoSomething()).Repeat.Any();

will fail to compile as we cannot convert from void to Rhino.Mocks.Expect.Action, we can easily fix by removing the () and using the following line

Expect.Call(session.DoSomething).Repeat.Any();

Equally if the ISession had a property named Result which was of type string we can declare the expectation as follows

Expect.Call(session.Result).Return("hello").Repeat.Any();

We can also setup an expectation on a method call using the following

session.Subscribe(null);
LastCall.IgnoreArguments().Return(null).Repeat.Any();

in this case the LastCall allows us to set our expectations on the previous method call, i.e. this is equivalent to our previous declaration for the expectation on the Subscribe method. This syntax is often used when dealing with event handlers.

Mocking Event Handlers

Let’s assume we have the following on the ISession

event EventHandler StatusChanged;

the idea being a session object may change, maybe to a disconnect state, and we want to have the Mapper respond to this in some way. Then we want to cause events to fire and then see whether the Mapper changes accordingly.

Okay, so let’s rewrite the Mapper constructor to look like the following

public Mapper(ISession session)
{
   Status = "Connected";
   this.session = session;

   session.StatusChanged += (sender, e) =>
   {
      Status = "Disconnected";
   };
}

The assumption is that we have a string property Status and that if a status change event is received the status should switch from Connected to Disconnected.

Firstly we need to handle the expectation of the += being called on the event in ISession, so our test would look like this

[Test]
public void TestMock()
{
   ISession session = repository.CreateMock<ISession>();

   session.StatusChanged += null;
   LastCall.IgnoreArguments();

   repository.ReplayAll();

   Mapper mapper = new Mapper(session);
   Assert.AreEqual("Connected", mapper.Status);
}

Notice we use LastCall to create an expectation on the += being called on the StatusChanged event. This should run without any errors.

Now we want to change things to see if the Mapper Status changes when a StatusChanged event takes place. So we need a way to raise the StatusChanged event. RhinoMocks includes the IEventRaiser interface for this, so rewriting our test as follows, will solve this requirement

[Test]
public void TestMock()
{
   ISession session = repository.CreateMock<ISession>();

   session.StatusChanged += null;
   LastCall.IgnoreArguments();

   IEventRaiser raiser = LastCall.GetEventRaiser();

   repository.ReplayAll();

   Mapper mapper = new Mapper(session);
   Assert.AreEqual("Connected", mapper.Status);

   raiser.Raise(null, null);

   Assert.AreEqual("Disconnected", mapper.Status);
}

Notice we use the LastCall.GetEventRaiser() to get an IEventRaiser. This will allow us to raise events on the StatusChanged event. We could simply combine the LastCall’s to form

IEventRaiser raiser = LastCall.IgnoreArguments().GetEventRaiser();

The call raiser.Raise(null, null) is used to actually raise the event from our test, the two arguments match the arguments on an EventHandler, i.e. an object (for the sender) and EventArgs.

More types of mocks

Along with CreateMock and DynamicMock you may notice some other mock creation methods.

What are the *MultiMocks?

CreateMultiMock and DynamicMultiMock allow us to create a mock (strict semantics for CreateMultiMock and loose for DynamicMultiMock) but supporting multiple types. In other words let’s assume our implementation of ISession is expected to support another interface, IStatusUpdate and this will have the event we’re previously declare, i.e.

public interface IStatusUpdate
{
   event EventHandler StatusChanged;
}

Now we change the Mapper constructor to allow it to check if the ISession also supports IStatusUpdate and only then subscribe to it’s event, for example

public Mapper(ISession session)
{
   Status = "Connected";
   this.session = session;

   IStatusUpdate status = session as IStatusUpdate;
   if (status != null)
   {
      status.StatusChanged += (sender, e) =>
      {
         Status = "Disconnected";
      };
   }
}

and finally let’s change the test to look like

[Test]
public void TestMock()
{
   ISession session = repository.CreateMultiMock<ISession>(typeof(IStatusUpdate));

   ((IStatusUpdate)session).StatusChanged += null;

   IEventRaiser raiser = LastCall.IgnoreArguments().GetEventRaiser();

   repository.ReplayAll();

   Mapper mapper = new Mapper(session);
   Assert.AreEqual("Connected", mapper.Status);

   raiser.Raise(null, null);

   Assert.AreEqual("Disconnected", mapper.Status);
}

As you can see, we’ve now created an mock ISession object which also supports IStatusUpdate.

PartialMock

The partial mock allows us to mock part of a class. For example, let’s do away with our Mapper and just write a test to check what’s returned from this new Session class

public class Session
{
   public virtual string Connect()
   {
      return "none";
   }
}

and our test looks like this

[Test]
public void TestMock()
{
   Session session = repository.PartialMock<Session>();

   repository.ReplayAll();

   Assert.AreEqual("none", session.Connect());
}

This will run and succeed when we use the PartialMock as it automatically uses the Session objects Connect method, but we can override this by using the following

[Test]
public void TestMock()
{
   Session session = repository.PartialMock<Session>();

   Expect.Call(session.Connect()).Return("hello").Repeat.Once();

   repository.ReplayAll();

   Assert.AreEqual("hello", session.Connect());
}

Now if instead we use CreateMock in the above this will still work, but if we remove the Expect.Call the mock does not fall back to using the Session Connect method but instead fails with an exception, ExpectationViolationException.

So if you need to mock a concrete object but have the code use the concrete class methods in places, you can use the PartialMock.

Note: You methods on the Session class need to be marked as virtual for the above to work

Obviously a PartialMultiMock can be used to implement more than one type.

Stubs

A stub is generally seen as an implementation of a class with minimal functionality, i.e. if we were to implement any of our ISession interfaces (shown in this post) and for properties we simply set and get from a backing store, methods return defaults and do nothing. Methods could return values but it’s all about minimal implementations and consistency. Unlike mocks we’re not trying to test behavior, so we’re not interested in whether a method was called once or a hundred times.

Often a mock with loose semantics will suffice but RhinoMock includes a specific type that’s created via

repository.Stub<ISession>();

the big difference between this and a dynamic mock is that in essense properties are all declared as

Expect.Call(session.Name).PropertyBehavior();

implicitly (PropertyBehavior is discussed in the next section). This means if we run a test using a dynamic mock, such as

ISession session = repository.DynamicMock<ISession>();
repository.ReplayAll();

session.Name = "Hello";

Assert.AreEqual(null, session.Name);

The property session.Name will be null even though we assigned it “Hello”. Using a stub, RhinoMocks gives us an implementation of the property setter/getter and thus the following would result in a test passing

ISession session = repository.DynamicMock<ISession>();
repository.ReplayAll();

session.Name = "Hello";

Assert.AreEqual("Hello", session.Name);

i.e. session.Name now has the value “Hello”.

Mocking properties

So, we’ve got the following interface

public interface ISession
{
   string Name { get; set; }
}

now what if we want to handle the getter and setter as if they were just simple setters and getters (i.e. implemented exactly as shown in the interface). Instead of creating return values etc. we can use a short cut

Expect.Call(session.Name).PropertyBehavior();

which basically creates an implementation of the property which we can now set and get from without setting full expectations, i.e. the following test shows us changing the Name property after the replay

[Test]
public void TestMock()
{
   ISession session = repository.CreateMock<ISession>();

   Expect.Call(session.Name).PropertyBehavior();

   repository.ReplayAll();

   session.Name = "Hello";
   Assert.AreEqual("Hello", session.Name);
}

Messing around with JustMock lite

I’ve been trying out JustMock Lite (hereon known as JML), from Telerik – the lite version is free and source available on Github. The package is installable via NuGet.

So let’s start with a simple Arrange-Act-Assert sample

IFeed feed = Mock.Create<IFeed>();

// arrange
Mock.Arrange(() => feed.Update(10)).Returns(true).OccursOnce();

// act
feed.Update(10);

// assert
Mock.Assert(feed);

The example above shows how we create a mock object based upon the IFeed interface. We then arrange the mocked methods etc. The next step in the sample above is where we use the mocked methods before finally setting assertions.

Note: We do not get a “mock” type back from Mock.Create as we would with a framework like Moq, but instead we get the IFeed which I rather like, not having to use the mock’s Object property to get the type being mocked. This is because in Moq the setup/arrange phase and for that matter the assert phase are all instance methods on the mock object, in JML we use static methods on the Mock class

Loose vs Strict

By default JML creates mocks with Behavior.Loose which means that we don’t need to supply all calls on the mock object upfront via the arrange mechanism. In other words, using Behavior.Loose simply means we might make calls on a mocked object’s methods (for example) without having to explicitly setup the Arrange calls and we’ll get default beaviour. Behavior.Strict means any calls we make on the mock object must have been set-up prior to being called on the mocked object.

Let’s look at an example of using JML’s strict behaviour

public interface IReader
{
   IEnumerable<string> ReadLine();
   string ReadToEnd();
}

[Fact]
public void ReadLine_EnsureCsvReaderUsesUnderlyingReader()
{
   IReader reader = Mock.Create<IReader>(Behavior.Strict);

   Mock.Arrange(() => reader.ReadLine()).Returns((IEnumerable<string>)null);

   CsvReader csv = new CsvReader(reader);
   csv.ReadLine();

   Mock.Assert(reader);
}

In the above, assuming (for the moment) that csv.ReadLine() calls the IReader ReadLine method, then all will be work. But if we remove the Mock.Arrange call we’ll get a StrictMockException as we’d expect as we’ve not setup the Arrange calls. Switching to Behavior.Loose in essence gives us a default implementation of the IReader ReadLine (as we’ve not explicitly provided one via the Mock.Arrange method) and all will work again.

As per other mocking frameworks this simply means if we want to enforce a strict requirement for each call on our mocked object to first be arranged, then we must do this explicitly.

JML also has two other behaviors, Behavior.RecursiveLoose which allows us to create loose mocking on all levels of the mocked object.

The Behavior.CallOriginal sets the mock object up to, by default, call the actual mocked object’s methods/properties. Obviously this means it cannot be used on an interface or abstract method, but what it does mean is that we can mock a class’s virtual method/property (JustMock elevated – the commercial version of JustMock – looks like it supports non virtual/abstract mocking on classes) and by default call the original object’s methods and only Arrange those methods/properties we want to alter.

For example, the following code will pass our test as JML will call our original code and does not require we Arrange the return on the property Name

public class Reader
{
   public virtual string Name { get { return "DefaultReader"; }}
}

[Fact]
public void Name_ShouldBeAsPerTheImplementation()
{
   Reader reader = Mock.Create<Reader>(Behavior.CallOriginal);

   Assert.Equal("DefaultReader", reader.Name);

   Mock.Assert(reader);
}

In some mocking frameworks, such as Moq, will intercept the Name property call and return the default (null) value instead (assuming we’ve not setup any returns ofcourse).

More on CallOriginal

Behavior.CallOriginal sets up the mocked object as, by default, calling the original implementation code, but we can also setup Arrange calls to call the original implementation more explicitly.

For example

public class Reader
{
   public virtual string GetValue(string key)
   {
      return "default";
   }
}

Reader reader = Mock.Create<Reader>();

Mock.Arrange(() => reader.GetValue(null)).Returns("NullReader");
Mock.Arrange(() => reader.GetValue("key")).CallOriginal();

Assert.Equal("NullReader", reader.GetValue(null));
Assert.Equal("default", reader.GetValue("key"));

Mock.Assert(reader);

So here when reader.GetValue is called with the argument “key” the original (concrete implementation) or the GetValue method is called.

Note: Moq also implements such a capability using the CallBase() method

More Moq

In the previous post we touched on the fundamentals of using Moq, now we’ll delve a little deeper.

Please note, MOQ version 4.20 has introduced a SponsoreLink which appears to send data to some third party. See discussions on GitHub.

Verifications

So we can verify that a method or property has been called using the following

var mock = new Mock<IFeed>();

FeedViewModel vm = new FeedViewModel(mock.Object);
vm.Update();

mock.Verify(f => f.Update());

This assumes that the vm.Update() calls the IFeed.Update().

But what if we are passing a mock into a view model and we want to verify a method on the mock is called but we don’t care about the specific arguments passed into the method. We can do the following (this example uses the IEventAggregator in Caliburn Micro)

var eventMock = new Mock<IEventAggregator>();

PostListViewModel vm = new PostListViewModel(eventMock.Object);

eventMock.Verify(e => e.Subscribe(It.IsAny<PostListViewModel>()), Times.Once);

In the above example, PostListViewModel’s constructor is expected to call the Subscribe method on the IEventAggregator. The actual implementation passes this into the Subscribe method, but we’ll ignore the argument, except that we’re expecting it to be of type PostListViewModel. The It.IsAny() handles this and the Times.Once simply verifies the Subscribe method was called just once.

Fundamental Moq

Moq is a fabulously simple yet powerful mocking framework.

Please note, MOQ version 4.20 has introduced a SponsoreLink which appears to send data to some third party. See discussions on GitHub.

For those unaware of what a mocking framework are and what they have to offer check out Wikipedia’s Mock Object post.

Here’s the fundamentals for starting to use Moq…

Let’s start with a simple interface for us to mock out

public interface IFeed
{
   string Name { get; set; }
   bool Update(int firstNPosts);
}

Now to mock this interface we can simply create a mock object based on the interface, as follows

IMock<IFeed> mock = new Mock<IFeed>();

obviously this is of little use unless we can set up some behaviours, i.e. what happens when a property or method etc. is called. So in the case of this interface we might want to return a known string for Name like this

IMock<IFeed> mock = new Mock<IFeed>();

mock.Setup(f => f.Name).Returns("SomeUniqueName");

// now use the mock in a unit test
IFeed feed = mock.Object;
Assert.Equal("SomeUniqueName", feed.Name);

The code mock.Object gets the actual interface from the mock, i.e. in this case an IFeed.

For methods we do much the same thing

IMock<IFeed> mock = new Mock<IFeed>();

mock.Setup(f => f.Update(10)).Returns(true);

// now use the mock in a unit test
IFeed feed = mock.Object;
Assert.True(feed.Update(10));

The examples above are a little contrived, after all we obviously are not really going to test that the mock we setup, but instead want to test something that uses the mock.

For example, what about if we have a FeedViewModel which takes an IFeed for display via WPF, something like the following

public class FeedViewModel : PropertyChangedBase
{
   private IFeed feed;
   private bool showProgress;
   private string error;

   public FeedViewModel(IFeed feed)
   {
      this.feed = feed;
   }

   public string Name { get { return feed.Name; } }

   public void Update()
   {
      ShowProgress = true; 
      bool updated = feed.Update(10);
      if(updated)
      {
         Error = "Failed to update";
      }
      ShowProgress = false;
   }

   public bool ShowProgress
   {
      get { return showProgress; }
      set { this.RaiseAndSetIfChanged(ref showProgress, value); }
   }

   public string Error
   {
      get { return error; }
      set { this.RaiseAndSetIfChanged(ref error, value); }
   }
}

Now we can test our FeedViewModel by creating a mock of the IFeed and passing into the FeedViewModel, allowing us to set up mock calls and check our expectations in tests.

The previous examples showed how we can use mocks to setup return values or just take the place of methods when they are called. We may, however, also wish to verify that our code did correctly call our mock objects one or more times – we can do this by marking the set up as Verifiable and then Verify the mocks at the end of the unit test, as per the following

Mock<IFeed> mock = new Mock<IFeed>();
mock.Setup(f => f.Posts).Returns(posts).Verifiable();

FeedViewModel model = new FeedViewModel(mock.Object);
Assert.Equal(posts.Count, model.PostCount);

mock.Verify();

In this example posts is a list of Post objects. Our view model has a property PostCount which uses the list of posts. We want to verify that the Posts property on our (new extended IFeed) was actually called. So we mark it as Verifiable() and at the end of the test we use mock.Verify(); to verify it was actually called.

We can be a little more explicit with our verifications, for example if we expect a method to be called twice, we can verify this as follows

Mock<IFeed> mock = new Mock<IFeed>();
mock.Setup(f => f.Update()).Returns(true);

FeedViewModel model = new FeedViewModel(mock.Object);
// do something on the model which will call the feed Update method

mock.Verify(f => f.Update(), Times.Exactly(2));

So this will fail verification (with a MockException) if the Update() method is not called exactly 2 times.

We also may wish to raise events on the mocked object to see what happens in our object under test, for example assume we have a StatusChanged event on our IFeed which the FeedViewModel should subscribe to and change an Updating property upon the relevant event arguments

Mock<IFeed> mock = new Mock<IFeed>();
FeedViewModel model = new FeedViewModel(mock.Object);

mock.Raise(f => f.StatusChanged += null, 
       new StatusChangedEventArgs(StatusChange.UpdateStarted));
Assert.True(model.Updating);

mock.Raise(f => f.StatusChanged += null, 
        new StatusChangedEventArgs(StatusChange.UpdateEnded));
Assert.False(model.Updating);

In this code we’re raising StatusChanged events on the IFeed mock and expecting to see the view model’s Updating property change.

xUnit in Action

I’ve not really seen any major purpose to use anything other than NUnit for unit testing in .NET since I switched 100% to it from a combination of mbUnit and NUnit a fair few years back. But recently I’ve had that urge we developers get, to try something different. So here’s a very brief post on using xUnit.

First off, if you need a VS2012 test runner for xUnit check out http://visualstudiogallery.msdn.microsoft.com/463c5987-f82b-46c8-a97e-b1cde42b9099.

So with NUnit we marked are test classes with [TestFixture]. xUnit doesn’t use anything to mark the class itself as a test class, it solely relies on the use of attributes on the test methods themselves (see http://xunit.codeplex.com/wikipage?title=Comparisons for a comparison of all the features between NUnit and xUnit).

The SetUp and TearDown of NUnit have been replaced with the ctor and IDisposable.Dispose on a test class and the TestFixtureSetUp and TestFixtureTearDown require you to implement IUseFixture on your test class.

So a simple example of a test is

[Fact]
public void Clear_MultipleBits_ShouldClearAllBits()
{
   Binary b = new Binary(4);
   b.Set(2, 4);
   b.Clear(2, 4);
   Assert.Equal(0, b.ToNumber());
}

So we use the Fact attribute to declare a test and Assert to verify the test. The Fact attribute also allows us to assign a DisplayName, test Name, a way to ignore (in this case Skip) a test and assign a string to this to tell everyone why the test was skipped and a timeout. So a Fact is pretty comprehensive.

On top of Fact the xUnit extensions library (available through NuGet) also adds some useful additions such as the Theory attribute which allows us to supply data to the test, for example

[
Theory, 
InlineData("1", 1),
InlineData("10", 2),
InlineData("11", 3),
InlineData("100", 4),
]
public void ToNumeric_TestVariousNumbers_BinaryStringShouldMatchDecimalValue(string s, int value)
{
   Assert.Equal(value, Binary.Parse(s).ToNumber());
}

In the above case we’ve marked the test as a Theory (which is also a Fact, hence no need for the Fact attribute). The InlineData attribute allows us to specify the arguments that will be passed into our method as arguments. So obviously you need the same number of values in the InLineData attribute as you have arguments on the method.

The Trait attribute is useful for grouping our tests (particularly if you’re using the xUnit test runner). So for example

[Fact, Trait("Bug", "1234")]
public void SomeTest()
{
}

In the above we create a new trait named Bug. In xUnit’s test runner this will create a new group within the Traits: window named Bug and add the item 1234.

The xUnit extensions project add a whole load of more attributes for use with xUnit tests, including various methods for bringing data into the test from different sources, such as from a separate class, a property on the test class, OleDb data, Excel and more.

One of the key difference with writing tests in xUnit comes with the way it handles exceptions. In NUnit we use the ExpectedException attribute. In xUnit the Assert.Throws wraps a specific method expecting an exception. For example

[Fact]
public void Divide_ExpectDivideByZeroException()
{
   Calculator c = new Calculator();
   Assert.Throws<ArgumentException>(() => c.Divide(4, 0));
}

xUnit also supports testing of async await code in the following manner

[Fact]
public async Task SomeAsyncAwaitTest()
{
   MyObject o = new MyObject();
   await o.Run();   
   Assert.True(MyObject.Completed);
}

Starting out with SpecFlow

SpecFlow is a BDD (Behaviour Driven Development) tool that supplies templates and integration for Visual Studio.

If you are new to BDD (or TDD or unit testing in general) go off to your favourite search engine and search. I’m sure there are far better definitions out there than I’ll probably come up with or try here. For those of you not wishing to waste a moment let’s start looking at SpecFlow.

The idea behind a feature is to define a test scenario in a more human readable way, in fact the perfect situation would be for the domain expert to write the scenarios to act as acceptance tests. SpecFlow is based upon Cucumber and uses the DSL Gherkin to allow the domain experts to write the scenarios.

Enough waffle let’s write a feature. We’ve got a Trade object which has a TradeDate and a StartDate. The StartDate should be calculated to be the TradeDate + 3 days.

Feature: Check the trade date is defaulted correctly
Scenario: Default the trade date
	Given Today's date is 23/10/2012
	When I create a trade
	Then the resultant start date should be 26/10/2012

The Feature text is just a name for the feature and can be followed by free form text to add documentation, in the above case I’ve not bothered. Instead I’ve gone straight to creating a Scenario.

A Scenario is basically the definition of what we want to test. Using the keywords Given, When, Then, And and But (note: I’ve not checked this is the full list). We can create the scenario for the test (don’t get too excited we still need to write the test) and then the Visual Studio integration will by default run the SpecFlowSingleFileGenerator custom tool against this feature file and create a feature.cs which contains the code for calling into your tests.

So we now have a feature file, potentially supplied by our domain expert. From this we get a .cs file generated which calls our code in the correct order and potentially passing arguments from the scenario to our test code. In the above case we want the dates to be passed to our tests. So now to write the code which will be run from the scenario generated code and here we add NUnit (or your preferred unit testing lib.) to check things are as expected.

[Binding]
public class Default_the_trade_date
{
   private Trade trade;
   private DateTime todaysDate;

   [Given("Today's date is (.*)")]
   public void Given_Todays_Date_Is(DateTime today)
   {
      todaysDate = today;
   }

   [When("I create a trade")]
   public void When_I_Create_A_CreateTrade()
   {
      trade = new Trade {TradeDate = todaysDate};
   }

   [Then("the resultant start date should be (.*)")]
   public void Then_Start_Date_Should_Be(DateTime dateTime)
   {
      Assert.AreEqual(trade.StartDate, dateTime);
   }
}

Note: I’ve named this class Default_the_trade_date this is the name I gave the scenario, this is not a requirement but makes it obvious what this code is for.

In the above you’ll notice the use of attributes which match the text in the scenario for each step, i.e. Given, When, Then. The text uses regular expressions, so matches the text to the scenario text but in this case the (.*) converts the dates into arguments to be passed into the test methods.

You can now right mouse click on your project or better still your .feature and select Run SpecFlow Scenarios. Unfortunately the above code will fail. As I’m using British date formats, i.e. dd/MM/yyyy and the default DateTime parser is en-US. So we need to edit the App.config for our scenarios to have

<language feature="en-GB" tool="{not-specified}" />

within the specFlow element.

If all went to plan, when you run the specflow scenarios the tests show green.

Note: If we have multiple features and/or scenarios which maybe use the same text, for example in the above code maybe we have several features and scenarios which use the Given “Today’s date is “. We can set the Scope attribute on the test classes and either mark the scenario with a tag

@tradeDate
Scenario: Default the trade date
   ...

or we can use the Feature and/or Scenario name such as

[Binding]
[Scope(Feature = "Check the trade date is defaulted correctly")]
public class Default_the_trade_date
{
   // code
}

See Scoped bindings for more on this and note the preference is towards not using such couplings.