Telerik blogs

Telerik JustMock received many new features in the service pack for the Q3 2011 release, enhancing the mocking framework’s impressive ability to mock almost everything. Let’s take a look at a few highlights of JustMock Q3 2011 SP.

Mock Inside a Threadpool

Mock objects can be accessed inside of another thread and work as expected.

var mockable = Mock.Create<Mockable>();
Mock.Arrange(() => mockable.IsMocked).Returns(true);

bool mocked = false;

var latch = new WaitLatch();

ThreadPool.QueueUserWorkItem(cookie =>
{
    try
    {
        mocked = mockable.IsMocked;
    }
    finally
    {
        latch.Signal();
    }
});

latch.Wait();

Assert.IsTrue(mocked);

The callback for ThreadPool.QueueUserWorkItem accesses the mock object, and the expected value is returned.

 

Asserting Occurrence in Extension Method

You can now ensure occurrences for extension methods to your mocked objects.

var sequence = Mock.Create<IEnumerable<int>>();

Mock.Arrange(() => sequence.First()).Returns(1).OccursOnce();

int result = sequence.First();

Assert.AreEqual(1, result);
Mock.Assert(sequence);

In this example, sequence.First() is only called once, passing the test.

IgnoreInstance in Fluent Extensions

Mehfuz Hossain’s article on Future Mocking with IgnoreInstance describes this feature added in JustMock Q3 2011, but you may have noticed he didn’t use the Fluent API. It is now present in these extensions, so the example in the original blog post can be written as follows:

var fakeUsed = Mock.Create<UsedClass>();

fakeUsed.Arrange(mock => mock.ReturnFive()).Returns(7).IgnoreInstance();
             
Assert.AreEqual(7, fakeUsed.ReturnFive());

Assert.AreEqual(7, new UsedClass().ReturnFive());

Whichever style you prefer, mocking tightly coupled class is simple with JustMock.

Invoke Call with Expression Argument with Dynamic Value

This feature gives you granular control over an arrangement utilizing a lambda expression.

var repository = Mock.Create<IBookRepository>();
var service = new BookService(repository);

var expected = new Book { Title = "Adventures" };

Mock.Arrange(() => repository.GetWhere(book => book.Id == 1))
    .Returns(expected)
    .MustBeCalled();

var actual = service.GetSingleBook(1);

Assert.AreEqual(actual.Title, expected.Title);

In this example, service.GetSingleBook(1) is calling its dependent repository with the expression: book => book.Id == id. The parameter ‘id’ is a dynamic value.

public class BookService
{
    private IBookRepository repository;

    public BookService(IBookRepository repository)
    {
        this.repository = repository;
    }

    public Book GetSingleBook(int id)
    {
        return repository.GetWhere(book => book.Id == id);
    }
}

JustMock will now match book.Id == 1 to book.Id == id when id is equal to one. The expression must match value types. For example, book.Id > 0 will never be called by service.GetSingleBook(1).

 

Better Access to Documentation

The Getting Started Guide and example projects are now found in the start menu under Telerik | JustMock.

image

These resources can help you find the solution to nearly any mocking scenario. The Getting Started Guide is the bulk of the English documentation. However, we view tests as documentation as well. That’s why the example projects provide a hands-on approach, unit testing JustMock itself.

The documentation is also available in Help3 for download in a separate ZIP file.

What’s Coming?

Our Q1 2012 Webinars are just around the corner. If you attend the What’s New in Tools for Better Code webinar, you can see live demos of the features mentioned in this blog post and much, much, more. One lucky winner from the Just* webinar will receive a Telerik Ultimate Collection worth $1999. More importantly, you will sharpen your ninja skills to write better code!


About the Author

Chris Eargle

is a Microsoft C# MVP with over a decade of experience designing and developing enterprise applications, and he runs the local .NET User Group: the Columbia Enterprise Developers Guild. He is a frequent guest of conferences and community events promoting best practices and new technologies. Chris is a native Carolinian; his family settled the Dutch Form region of South Carolina in 1752. He currently resides in Columbia with his wife, Binyue, his dog, Laika, and his three cats: Meeko, Tigger, and Sookie. Amazingly, they all get along... except for Meeko, who is by no means meek.

Related Posts

Comments

Comments are disabled in preview mode.