Moq and the parameters that you pass

I create a MOQ object

pass it to my class

call it and pass the object.

How can I access this object to find out if the desired properties are set?

+3
source share
3 answers

If I read your question correctly, do you seem to have this situation?

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

In this case, you can assert the arguments passed to the Mocked interface using the It.Is method, which takes 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));

}
+2
source

To do this, use the method .VerifySet()for the mock object. For instance,

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

If it was not installed correctly, it will throw an exception.

Read more in quick start here .

+1

, , .

Moq "", , . , .

var myMock = new Mock<MyObj>();

var myProp = myMock.Object.MyProperty;
+1
source

Source: https://habr.com/ru/post/1748577/


All Articles