3

I'm going to simplify a bit the problem:

In my tests, I use a mocked object (I mocked it because it calls a bdd) which I give in parameters to a method of another object (not mocked) whose purpose is to modify a property of this mocked object.

ModifyingClass myModifyingClass = new ModifyingClass();
Mock<ToModifyClass> mockedClass = new Mock<ToModifyClass>();
mockedClass.Setup(mc => mc.name).Returns("Test1");
myModifyingClass.modify(mockedClass.Object);

The method modify then try to set the property name of the mocked object, but it won't work, so maybe it's the normal behavior but I really need to test if all of this work and if the method modify set the property as I want.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • What happens when you run the test? Where the error occurs? – Janis S. Jun 22 '17 at 09:24
  • What mocking library is this? Regardless, you should stub not mock that property if you want to use it like a real property - how exactly you do that depends on the library. – Alex Paven Jun 22 '17 at 09:24
  • Janis. S. I debug the test and no error happens, the property is just not modified. – Jean Pougetoux Jun 22 '17 at 09:25
  • How do you know it is not modified, do you have an assert statement? – Janis S. Jun 22 '17 at 09:27
  • Alex Paven, it's just the usual library Moq, include with visual studio, I didn't studied stud yet, I'll look at that maybe it can be better for my problem – Jean Pougetoux Jun 22 '17 at 09:27
  • No Janis S. I debug line by line and check if the property as been changed. When i send a real ToModifyClass (not mocked), the property is set and when it's a mock with a setup it's not – Jean Pougetoux Jun 22 '17 at 09:28
  • 1
    Please have a look here: https://stackoverflow.com/questions/16816551/how-to-assign-values-to-properties-in-moq, you would need to call SetupAllProperties() – Janis S. Jun 22 '17 at 09:33
  • Janis S. I tried it It work if i don't setup the returns as i did in my example but I need to do it, I think I'm going to find another method because it's obviously not working like that :/ Thanks for your help ! – Jean Pougetoux Jun 22 '17 at 09:40

2 Answers2

1

As mentioned in the comments you need to setup the mocked class differently in order to retain the values passed to properties.

Reference Moq - Quickstart

Stub all properties on a mock (not available on Silverlight):

mock.SetupAllProperties();

The example test provided would then look like...

//Arrange
var myModifyingClass = new ModifyingClass();
var mockedClass = new Mock<ToModifyClass>();
mockedClass.SetupAllProperties(); //<-- this will allow property changes to be retained.
var model = mockedClass.Object;
//set the property now that it can be set
model.name = "Test1";
var expected = "expected value here";

//Act
myModifyingClass.modify(model);

//Assert
var actual = model.name;
Assert.AreEqual(expected, actual);
//... other assertions
Nkosi
  • 235,767
  • 35
  • 427
  • 472
0

In Moq one would need to setup the getter.

mockedClass.SetupGet(mc => mc.name).Returns("Test1");
Janis S.
  • 2,526
  • 22
  • 32
  • It won't work either, I think I get the "Test1" even if I set the property because of the setup/setupget. I'm finally not going to use setup and set the property myself, like that it works, thank you very much for your help – Jean Pougetoux Jun 22 '17 at 09:47