views:

60

answers:

3

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?

+1  A: 

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.

James Kolpack
+1  A: 

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;
womp
+1  A: 

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

}
sighohwell