开发者

Moq and the parameters you pass

开发者 https://www.devze.com 2023-01-02 13:47 出处:网络
I create a MOQ object pass it to my class call it and pass in an object. How can I get access to this object to开发者_高级运维 see if the right properties are set?If I\'m reading your question corr

I create a MOQ object

pass it to my class

call it and pass in an object.

How can I get access to this object to开发者_高级运维 see if the right properties are set?


If I'm reading your question correctly you seem to have a situation like this?

public void DoTheCalculation(ICalculator calculator) {
   calculator.Calculate(this /* Or any other object */);
}

In which case you can assert upon the arguments passed to a Mocked interface using the It.Is method that accepts a predicate:

[TestMethod]
public void DoTheCalculation_DoesWhateverItShouldDo() {
     Mock<ICalculator> calcMock = new Mock<ICalculator>();
     CalculationParameters params = new CalculationParmeters(1, 2);
     params.DoTheCalculation(calcMock.Object);

     calcMock.Verify(c => c.Calculate(It.Is<CalculationParameters>(
                         c => c.LeftHandSide == 1 
                              && c.RightHandSide == 2));

}


You use the .VerifySet() method on the mock object for this. For instance

mockObject.VerifySet(o => o.Name = "NameSetInMyClass");

If it wasn't set properly, it will raise an exception.

Read more in the quick start here.


Your question is kind of confusing, it's unclear what object you're referring to in the last sentence.

However, every Moq wrapper has an "Object" property that is of the type that you are mocking. You can access it by simply viewing that property.

var myMock = new Mock<MyObj>();

var myProp = myMock.Object.MyProperty;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号