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
Firstly, great tool! It's really helped me a lot so far, and I never go a day without it.
Summary:
When using reference types as parameters to mocked methods, and altering internal variables of parameter, calling Mock.Verify on the method can fail, because Mock stores the reference to the parameter, and not a copy of it.
Code setup:
privateclassPerson{publicstringName{get;set;}publicstringSurname{get;set;}}publicinterfaceIApi{voidAdd(objectobj);}[Fact]publicvoid Test(){
var obj =new Person(){Name ="Name", Surname ="Surname"};
var mock =newMock<IApi>();varmockedObject= mock.Object;
mockedObject.Add(obj);
obj.Name ="Shaun";// Name, in this case is equal to "Shaun"// Verification fails because the "Name" properties differ
mock.Verify(e => e.Add(new Person(){Name="Name",Surname="Surname"}), Times.Once);}
Expected outcome:
The verification should succeed, even though the object changed. I expect that the parameters that were passed be deep cloned to be used for verification
Current outcome:
Verification fails
Moq version: 4.16.1
The text was updated successfully, but these errors were encountered:
We get this report all the time. I'll let you use the search facility to find similar issues (along with my responses), I'll just say here that this behavior is by design (the usual .NET equality semantics apply) and it won't be changed.
Perfect, thanks! Then I know what to do. I see one similar "issue" was reported and closed recently, so sorry about that. I'm not used to Github's structure yet, coming from an Azure DevOps background
If anyone finds this issue and is experiencing the same problem, please see #1319; the last code example in the PR description shows a way how to solve this.
Hello!
Firstly, great tool! It's really helped me a lot so far, and I never go a day without it.
Summary:
When using reference types as parameters to mocked methods, and altering internal variables of parameter, calling Mock.Verify on the method can fail, because Mock stores the reference to the parameter, and not a copy of it.
Code setup:
Expected outcome:
The verification should succeed, even though the object changed. I expect that the parameters that were passed be deep cloned to be used for verification
Current outcome:
Verification fails
Moq version: 4.16.1
The text was updated successfully, but these errors were encountered: