Reputation: 2328
In our solution we used to have a really ancient version of JustMock. We wrote around 7000 tests and one pattern that would sometimes emerge is to have a default mock of a certain interface for all test-cases and in some tests "override" that default mock.
This means that in a few hundred tests we create a mock and arrange it and then create another mock of the same type and arrange that too. Then we would make an assertion on that second mock.
Some months ago we updated to the current version and now these multiple mocks of the same type seem not to work anymore. Take the example below. I create two Mocks of the same type and assert that the second mock is called once. With a 2012 version of JustMock this tests fails but with a more recent it does not fail.
So what is JustMock doing, that prevents me from making multiple mocks of the same type and asserting on them? And why does it do that?
public interface IFoo
{
void Bar();
}
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var bar = Mock.Create<IFoo>();
Mock.Arrange(() => bar.Bar()).OccursOnce();
var foo = Mock.Create<IFoo>();
Mock.Arrange(() => foo.Bar()).OccursOnce();
Mock.Assert(foo); // Succeeds in recent versions, fails in older versions as I would expect
//Mock.Assert(bar); // Would fail as expected
}
}
Upvotes: 2
Views: 499
Reputation: 538
Indeed this seems like a regression with a previous version of JustMock. I have logged this as a bug report into the JustMock feedback portal. Here is a link if you would like to subscribe for status updates: Mock assert doesn't fail when there are multiple mocks with occurrence expectations that are not satisfied. The problem is fixed with JustMock 2019.1.115.2 release.
Upvotes: 1
Reputation: 18655
I've just had the same problem! You can fix it by using the Telerik.JustMock.Helpers
extensions:
var bar = Mock.Create<IFoo>();
bar.Arrange(x => x.Bar()).OccursOnce();
var foo = Mock.Create<IFoo>();
foo.Arrange(x => x.Bar()).OccursOnce();
Now JustMock
won't make a mistake finding the wrong instance in the closure because you're specifying it to the expression directly.
Upvotes: 0