簡體   English   中英

如何使用Rhino Mocks檢查傳遞給方法的值

[英]How can I use Rhino Mocks to inspect what values were passed to a method

我是嘲笑的新手,我很難解決UnitTesting的問題。

說我有這個代碼:

public class myClass{

    private IDoStuff _doer;

    public myClass(IDoStuff doer){
        _doer = doer;
    }

    public void Go(SomeClass object){

        //do some crazy stuff to the object

        _doer.DoStuff(object) //this method is  void too
    }
}

好的,所以我想UNIT測試Go方法。 一旦得到它,我不關心_doer對象對對象的作用。

但是,我確實想檢查_doer對象收到了什么。

在PSEUDO代碼我想實現這個:

[Test]
public void MyTest()
{
    IDoStuff doer = Mocker.Mock<IDoStuff>();
    Guid id = Guid.NewGuid();

    //test Go method
    new MyClass(doer).Go(new SomeClass(){id = id});

    Assert.AreEqual(id,MockingFramework.Method(DoStuff).GetReceived<SomeClass>().id);
}

這是否可以使用Rhino,如果是這樣,我該如何實現它?

干杯

使用新的Arrange / Act / Assert語法:

[Test]
public void MyTest()
{
    // arrange
    IDoStuff doer = MockRepository.GenerateStub<IDoStuff>();
    MyClass myClass = new Myclass(doer);
    Guid id = Guid.NewGuid();

    // act
    myClass.Go(new SomeClass(){id = id});

    // assert
    doer.AssertWasCalled(x => x.DoStuff(
        Arg<Someclass>.Matches(y => y.id == id)));
}

所有這些答案提供了各種方法來做你想要的,所有這些都有效。 還有一件事需要注意。 如果您需要獲得真正的“低級別”並檢查傳遞給任何存根/ GetArgumentsForCallsMadeOn方法的參數,則可以使用GetArgumentsForCallsMadeOn

它返回object [] []時有點亂。 你這樣使用它(假設你將stubber.InsertData存根為接受null):

var args = stubber.GetArgumentsForCallsMadeOn(s => s.InsertData(null));

args [0]是第一次調用時傳遞給InsertData的參數數組。

args [1]是第二次調用時傳遞給InsertData的參數數組。

等等...

因此,如果您希望看到作為第一次調用某個方法的第二個參數傳遞的整數值,您可以:

var check = (int) args[0][1];

同樣,我建議使用以前的方法之一,但是如果您需要真正關閉並查看參數,這是可用的。

我認為你所擁有的是好的所以它是:

IDoStuff doer = MockRepository.GenerateMock<IDoStuff>();

然后通過以下方式設定期望:

doer.Expect(() => DoStuff(id));

然后在最后:

doer.VerifyAllExpectations();

從Lee的回答中注明,您還可以執行以下操作:

doer.Expect(d => d.DoStuff(Arg<int>.Is.GreaterThan(5))

要么

doer.Expect(d => d.DoStuff(Arg<CustomObject>.Matches(x => x.CustomProperty == "Beef")));

或者類似的測試,當您不希望使用Arg和Arg對象進行精確的參考比較時。

只有一個建議:

來自Wim CoenenPatrick Steele的解決方案都是正確的,但是,對於第一個解決方案,當只有一個參數時非常快,當測試失敗時會出現錯誤的錯誤消息。

這是我的函數的消息,有兩個參數:

IProductChecker.MustPublish(等於2,等於123X); 期望#1,實際#0。

現在,這兩個參數中的哪一個是錯誤的? 如果參數更多呢?

我用這段代碼准備了測試:

//ARRANGE
const string CLASSCODE = "ABC";
const string SUBCLASSCODE = "123X";
var expected = new [] {CLASSCODE, SUBCLASSCODE};

//ACT
SUT.CallMyFunction(chkMock);
var actual = chkMock.GetArgumentsForCallsMadeOn(m => m.MustPublish(null, null))[0];

//Assert
CollectionAssert.AreEqual(expected, actual);
//instead of
//chkMock.AssertWasCalled(m => m.MustPublish(Arg<string>.Is.Equal("2"), Arg<string>.Is.Equal(SUBCLASSCODE)));

因此,在這種情況下,消息是:

CollectionAssert.AreEqual失敗。 (索引0處的元素不匹配。)

你好

如果您只是想測試MyClass實例將其參數傳遞給doer.Go那么您可以設置一個期望:

SomeClass obj = new SomeClass { id = id };

doer.Expect(d => d.DoStuff(obj));

//test go method
new MyClass(doer).Go(obj);

doer.VerifyAllExpectations();

但是,如果要檢查它是否為某個屬性傳遞了某個特定值的可能不同的對象,則可以使用約束:

doer.Expect(d => d.DoStuff(null))
    .IgnoreArguments()
    .Constraints(Property.Value("Id", expectedId));

暫無
暫無

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

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