01.
using
System;
02.
using
System.Collections.Generic;
03.
using
Microsoft.VisualStudio.TestTools.UnitTesting;
04.
using
Telerik.JustMock;
05.
06.
namespace
JustMockTestProject
07.
{
08.
public
interface
IFoo
09.
{
10.
IBar Bar {
get
; }
11.
}
12.
13.
public
interface
IBar : IEnumerable<Object>
14.
{
15.
IBaz GetBaz();
16.
}
17.
18.
public
interface
IBaz {}
19.
20.
[TestClass]
21.
public
class
JustMockTest
22.
{
23.
[TestMethod]
24.
public
void
TestMethod1()
25.
{
26.
var foo = Mock.Create<IFoo>(Behavior.RecursiveLoose);
27.
var bar = Mock.Create<IBar>(Behavior.RecursiveLoose);
28.
29.
Assert.IsNotNull(bar.GetBaz());
// passes
30.
Assert.IsNotNull(foo.Bar.GetBaz());
// fails
31.
}
32.
}
33.
}
Expected Behavior: RecursiveLoose mocking behavior will cause every method to return a mocked object for reference types.
Actual Behavior: When RecursiveLoose runs into an interface that derives from IEnumerable<T>, the returned mocked object does not get recursively mocked causing NullReferenceException when a test attempts to access members on the mocked object (IBaz in the example above).
The above is a simplified test case to narrow down the source of the bug. If I have an IEnumerable<T> property on IFoo instead, then the returned object is correctly mocked (GetEnumerator doesn't return null).
As shown in the above example on line 29, mocking an IBar directly works. It is only when attempting to recursively mock an object that derives from IEnumerable that the problem occurs.