Xander
Xander

Reputation: 9171

Moq and the parameters you pass

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?

Upvotes: 3

Views: 1122

Answers (3)

John Foster
John Foster

Reputation: 8755

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

}

Upvotes: 2

womp
womp

Reputation: 116987

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;

Upvotes: 1

James Kolpack
James Kolpack

Reputation: 9382

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.

Upvotes: 1

Related Questions