chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
I'm having trouble changing faked behavior over multiple calls. The behavior is not affected by subsequent calls to WhenCalled(). See my failing example below. I'm using Isolator 5.3.1.0. Any ideas? Thanks, Paul.


public interface IValueSource {
  int Value {
    get;
  }
}


public class ClassUnderTest {
  public int ReturnValue(IValueSource provider) {
    return provider.Value;
  }
}



[Test]
public void TestChangingBehavior() {
    ClassUnderTest obj = new ClassUnderTest();
    IValueSource source = Isolate.Fake.Instance<IValueSource>();
    Isolate.WhenCalled(() => source.Value).WillReturn(1);
    Assert.AreEqual(1, obj.ReturnValue(source));
    Isolate.WhenCalled(() => source.Value).WillReturn(2);
    Assert.AreEqual(2, obj.ReturnValue(source)); //this fails because the returned value is still 1.
}
asked by woodward (1.7k points)

2 Answers

0 votes
It appears that all of the WhenCalled() calls must be made before any calls to the faked method/property. Changing to the following fixed the problem.

public void TestChangingBehavior() { 
    ClassUnderTest obj = new ClassUnderTest(); 
    IValueSource source = Isolate.Fake.Instance<IValueSource>(); 
    Isolate.WhenCalled(() => source.Value).WillReturn(1); 
    Isolate.WhenCalled(() => source.Value).WillReturn(2); 
    Assert.AreEqual(1, obj.ReturnValue(source)); 
    Assert.AreEqual(2, obj.ReturnValue(source));
}


I think the previous code communicates intentions better, but this works.
answered by woodward (1.7k points)
0 votes
When writing an isolated unit test you should have three parts Arrange Act and Assert.
In the first test Arrange and Act stages are mixed and so the repeat feature you're using (return value x then return value y) returns the first value twice instead of once.

We have assumed in the past that a unit test should follow AAA convention and have separate arrange and act parts. I will add a feature request to enable such tests as the first example to work when using Isolator
answered by dhelper (11.9k points)
...