簡體   English   中英

如何使用FakeItEasy驗證方法參數以及是否進行了調用

[英]How to verify method parameters with FakeItEasy as well as that a call has been made

我正在使用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);
}

馬庫斯(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傳入someInt作為值2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM