Reputation: 385
So I'm trying to mock an interface that contains a property that my unit test has to set initially, which is then altered by the subject under test, and then my assertion looks at the result.
The code under test looks like this:
public class Foo
{
IEnumerable<int> IntList { get; set; }
public void Bar(IBaz baz)
{
if (baz == null)
throw new ApplicationException();
if (baz.IntList == null)
baz.IntList = new List<int>();
this.IntList = baz.IntList;
}
}
I'm mocking my interface in a factory like this:
public class BazFactory
{
public IEnumerable<int> IntList { get; set; }
public IBaz Create()
{
IBaz baz = Mock.Create<IBaz>();
baz.Arrange(x => x.IntList).Returns(this.IntList);
return baz;
}
}
And my unit test consumes it all like this:
[Fact]
public void WithNullIntList_InitializesNew()
{
//Arrange
Foo foo = new Foo();
BazFactory factory = new BazFactory();
IBaz baz = factory.Create();
//Act
foo.Bar(baz);
//Assert
Assert.NotNull(foo.IntList);
}
Obviously, this isn't working, because though foo.Bar()
tries to change baz.IntList
, it still returns factory.IntList
, which hasn't changed.
What I need is to be able to arrange baz.IntList
so that when it is set, the value passed to it is diverted to factory.IntList
instead. I have only seen documentation on how to do this with a variable inside the mock class, and how to do it with a method, but not with a property's setter.
I feel like I should be able to add a line to BazFactory.Create()
like this, but the following code doesn't work.
baz.ArrangeSet(x => this.IntList = x.IntList);
How can I express "the value passed to baz.IntList
" in the context of an action?
If it makes a difference, I am using the Lite version of JustMock.
Upvotes: 1
Views: 292