You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This was posted to StackOverflow here. The person who responded, known as 'stakx' seems to be on the right track.
While I was experimenting to resolve a different situation with Moq, I attempted to use SetupSet to resolve. This uncovered another potential problem.
When I use SetupSet on a property along with a Setup on a Method, Moq seems to 'forget' that the Setup on the Method has been done.
Here is sample code, very simple:
publicclassPrancer{publicPrancer(boolpIsMale){IsMale=pIsMale;
ExecuteMe();}privatebool_IsMale;publicvirtualboolIsMale{get{returnthis._IsMale;}privateset{this._IsMale =value;}}privatebool_Antlers;publicvirtualboolAntlers{get{returnthis._Antlers;}set{this._Antlers =value;}}publicvirtualvoidExecuteMe(){thrownew Exception("Why am I here?");}}
Here are the unit tests:
publicclassPrancerTests{[Fact]publicvoidAntlers_NoSetup(){// Arrangeinttemp=0;// create mock of class under testvarsut=newMock<Prancer>(true){CallBase=true};
sut.Setup(x => x.ExecuteMe()).Callback(()=>temp=1);// nullify// Act
sut.Object.Antlers =true;// Assert
sut.VerifySet(x => x.Antlers =true);
Assert.Equal(1, temp);}[Fact]publicvoidAntlers_SetupProperty(){// Arrangeinttemp=0;// create mock of class under testvarsut=newMock<Prancer>(true){CallBase=true};
sut.SetupProperty(x => x.Antlers,false);
sut.Setup(x => x.ExecuteMe()).Callback(()=>temp=2);// nullify// Act
sut.Object.Antlers =true;// Assert
sut.VerifySet(x => x.Antlers =true);
Assert.Equal(2, temp);}[Fact]publicvoidAntlers_SetupSet(){// Arrangeinttemp=0;// create mock of class under testvarsut=newMock<Prancer>(true){CallBase=true};
sut.Setup(x => x.ExecuteMe()).Callback(()=>temp=3);// nullify
sut.SetupSet(x => x.Antlers =true);// Act
sut.Object.Antlers =true;// Assert
sut.VerifySet(x => x.Antlers =true);
Assert.Equal(3, temp);}}
The unit test where I use SetupSet reports the Exception ("Why am I here?") thrown in method ExecuteMe() which proves that the method ExecuteMe() executed even when there was a Setup(x => x.ExecuteMe()) to prevent it. The other two unit tests pass (and apparently do not execute ExecuteMe()).
I even attempted to put a Callback on the Setup for ExecuteMe(), but same result. I also reversed the order (in code) of the Setup and SetupSet, to no avail.
Any thoughts why the SetupSet could have affected the method Setup?
The text was updated successfully, but these errors were encountered:
This was posted to StackOverflow here. The person who responded, known as 'stakx' seems to be on the right track.
While I was experimenting to resolve a different situation with
Moq
, I attempted to useSetupSet
to resolve. This uncovered another potential problem.When I use
SetupSet
on a property along with aSetup
on a Method, Moq seems to 'forget' that theSetup
on the Method has been done.Here is sample code, very simple:
Here are the unit tests:
The unit test where I use
SetupSet
reports theException
("Why am I here?") thrown in methodExecuteMe()
which proves that the methodExecuteMe()
executed even when there was aSetup(x => x.ExecuteMe())
to prevent it. The other two unit tests pass (and apparently do not executeExecuteMe()
).I even attempted to put a
Callback
on the Setup forExecuteMe()
, but same result. I also reversed the order (in code) of theSetup
andSetupSet
, to no avail.Any thoughts why the
SetupSet
could have affected the methodSetup
?The text was updated successfully, but these errors were encountered: