1

I'm using FakeItEasy to do some testing, but I have run into a problem. When testing that the expected data is sent to a faked service, I would like to be able to see the error data as well. Right now I only see that the call never happened. Maybe I'm setting it up all wrong, but then I'd like some hints on how to correct it. :)

My case is: A call to the same service twice, with different values. I want separate tests to verify each call. If any of the parameters, doesn't have the expected value, I would like to get an error message stating that. Similar to when you do an Assert.AreEqual().

Right now I only get "Call did not happen", which is totally understandable, because I realise that is what I'm testing. But I would like to be able to verify that a specific call has only been made once, and if it did not happen I would like to see which values were used to not make it happen.

I used this solution: http://thorarin.net/blog/post/2014/09/18/capturing-method-arguments-on-your-fakes-using-fakeiteasy.aspx when I only had one call, but with two calls it doesn't work.

 [TestFixture]
public class TestClass
{
    [Test]
    public void TestOne()
    {
        // Arrange
        var fake = A.Fake<IBarservice>();
        var a = new Foo(fake);

        // Act
        a.DoStuff(1);

        //Assert
        A.CallTo(() => fake.DoOtherStuff(A<int>.That.Matches(x => x == 2))).MustHaveHappened(Repeated.Exactly.Once);
    }

    [Test]
    public void TestTwo()
    {
        // Arrange
        var fake = A.Fake<IBarservice>();
        var a = new Foo(fake);

        // Act
        a.DoStuff(1);

        //Assert
        A.CallTo(() => fake.DoOtherStuff(A<int>.That.Matches(x => x == 3))).MustHaveHappened(Repeated.Exactly.Once);
    }
}

public class Foo
{
    private readonly IBarservice _barservice;

    public Foo(IBarservice barservice)
    {
        _barservice = barservice;
    }

    public void DoStuff(int someInt)
    {
        someInt++;
        _barservice.DoOtherStuff(someInt);
        // I should have increased someInt here again, but this is a bug that my tests catches
        _barservice.DoOtherStuff(someInt);
    }
}

public interface IBarservice
{
    void DoOtherStuff(int someInt);
}
Markus
  • 1,614
  • 1
  • 22
  • 32

1 Answers1

1

Markus, I had a comment that I've redacted, since I made an error.

You say you only get "Call did not happen" and

… I would like to be able to verify that a specific call has only been made once, and if it did not happen I would like to see which values were used to not make it happen.

I fear I don't understand what information you were hoping for, since when I run TestOne, I get

FakeItEasy.ExpectationException

  Assertion failed for the following call:
    FakeItEasyQuestionsVS2015.IBarservice.DoOtherStuff(<x => (x == 2)>)
  Expected to find it exactly once but found it #2 times among the calls:
    1: FakeItEasyQuestionsVS2015.IBarservice.DoOtherStuff(someInt: 2) repeated 2 times
    ...

This says that the call DoOtherStuff was made twice, with someInt passed in as the value 2 each time.

Blair Conrad
  • 233,004
  • 25
  • 132
  • 111