How to fake an action <> with FakeItEasy

I work with the FakeItEasy library to create fakes for my unit tests.

I have ClassUnderTestone on which I want to test a method MethodToTest(Data dataObject). This method calls the interface method that I want to fake:

public interface IFoo
{
  void Execute(Action<IDataAccess> action);
}

public class ClassUnderTest
{
  private IFoo _foo;

  public ClassUnderTest(IFoo foo)
  {
    _foo = foo;
  }

  public void MethodToTest(Data dataObject)
  {
    _foo.Execute(dataAccess => dataAccess.Update(dataObject));
  }
}

public interface IDataAccess
{
  void Update(Data data);
}

public class Data
{
  public int Property { get; set; }
}

In my unit tests, I want to check if the testing method correctly names the interface correctly (with the correct property value):

[TestClass]
public class UnitTest1
{
  [TestMethod]
  public void TestMethod1()
  {
    var foo = A.Fake<IFoo>(x => x.Strict());
    A.CallTo(() => foo.Execute(dataAccess => dataAccess.Update(A<Data>.That.Matches(d => d.Property == 20))));      

    var cut = new ClassUnderTest(foo);

    cut.MethodToTest(new Data { Property = 20 });      
  }
}

But there is something wrong with this test. I get an exception:

Test method TestProject1.UnitTest1.TestMethod1 throws an exception: FakeItEasy.ExpectationException: a call to the unconfigured Execution method of a strict fake.

Does anyone have an idea on how I need to configure the operator correctly CallTo()?

!

+4
2

, @rhe1980.

, :

, Action , , , , Execute, IDataAccess , . , FakeItEasy, !

:

[TestMethod]
public void TestMethod1()
{
    // Arrange
    var foo = A.Fake<IFoo>(x => x.Strict());

    var fakeDataAccess = A.Fake<IDataAccess>();

    A.CallTo(() => foo.Execute(A<Action<IDataAccess>>.Ignored))
                    .Invokes((Action<IDataAccess> action)=>action(fakeDataAccess));

    var cut = new ClassUnderTest(foo);

    // Act
    cut.MethodToTest(new Data { Property = 20 });

    // Assert
    A.CallTo(() => fakeDataAccess.Update(A<Data>.That.Matches(d => d.Property == 20)))
        .MustHaveHappened();
}

, .

+4

, - :

A.CallTo(() => foo.Execute(A<Action<IDataAccess>>.Ignored).MustHaveHappened();
+1

All Articles