2016-04-26 54 views
0

我正在使用FakeItEasy进行一些测试,但遇到了问题。 当测试预期数据发送到伪造服务时,我希望能够看到错误数据。 现在我只看到电话从未发生过。 也许我把它全部弄错了,但是我想知道如何改正它。 :)如何使用FakeItEasy验证方法参数以及发出呼叫

我的情况是: 两次调用相同的服务,具有不同的值。 我想单独测试来验证每个呼叫。 如果有任何参数,没有预期的值,我想获得一个错误消息说明。与您执行Assert.AreEqual()时类似。

现在我只能得到“呼叫没有发生”,这是完全可以理解的,因为我意识到这就是我正在测试的。但我希望能够验证一个特定的电话只有一次,如果它没有发生,我想看看哪些价值被用来不发生。

我用这个解决方案:http://thorarin.net/blog/post/2014/09/18/capturing-method-arguments-on-your-fakes-using-fakeiteasy.aspx 当我只有一个电话,但有两个电话不起作用。

[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); 
} 

回答

1

Markus,因为我犯了一个错误,所以我有一条评论,我已经编辑了。

你说你只能得到“通话没有发生”和

...我想能够验证一个特定的呼叫只进行了一次,如果它没有发生,我想看看哪些值被用来不发生。

我怕我不明白你所希望看到什么样的信息,因为当我运行TestOne,我得到

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 
    ... 

这就是说通话DoOtherStuff作出两次,someInt作为传递每次值为2