This is a migrated thread and some comments may be shown as answers.

setting testing value which has been set earlier using .Returns

3 Answers 18 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Gary
Top achievements
Rank 1
Gary asked on 24 Jun 2015, 02:26 PM

Hi, I am testing a method whereby a value is set in the method. Initially I set the return value using JustMock. This is SharePoint by the way.

01.// arrange
02.SPItemEventProperties properties = Mock.Create<SPItemEventProperties>(Behavior.Strict);
03.Mock.Arrange(() => properties.BeforeProperties["Title"]).Returns("Old Title");
04.Mock.Arrange(() => properties.AfterProperties["Title"]).Returns("New Title");
05. 
06.// act
07.Person p = new Person();
08.p.DoSomething(properties);     // -> properties.AfterProperties["Title"] = "abc";
09. 
10.// assert
11.Assert.AreEqual("abc", properties.AfterProperties["Title"].ToString()); // fails here since AfterProperties is still "New Title"

I realize that the .Return is causing the same value to be returned. How can I check that the correct value is being set inside the method / can I get the value which has been set in the method back in the assert.arequal?

 

Thanks,

Gary.

 

3 Answers, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 29 Jun 2015, 10:36 AM
Hi Gary,

To check if a certain property is assigned an expected value, you will have to use the Mock.ArrangeSet method of JustMock. For example, in your case, you will have something like the following:
// arrange
SPItemEventProperties properties = Mock.Create<SPItemEventProperties>(Behavior.Strict);
Mock.Arrange(() => properties.BeforeProperties["Title"]).Returns("Old Title");
Mock.ArrangeSet(() => properties.AfterProperties["Title"] = "abc");
 
// act
Person p = new Person();
p.DoSomething(properties);     // -> properties.AfterProperties["Title"] = "abc";
  
// assert
Mock.Assert(properties);

More about this can be found here. I hope this helps.

Regards,
Kaloyan
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Gary
Top achievements
Rank 1
answered on 29 Jun 2015, 12:00 PM

Hi Kaloyan,

This solution won't work if the code you are calling is wrapped in a try catch. 

 for example:

1.public void DoSomething(SPItemEventProperties) {
2.  try {
3. 
4.      poperties.AfterProperties["Title"] = "badstring";
5.  }
6.  catch(Exception e) {
7.  }
8. }

 

Is there another solution for this problem, or a workaround?

Regards,

Gary

0
Stefan
Telerik team
answered on 30 Jun 2015, 11:46 AM
Hi Gary,

Strict mocks don't mix well with exception catch-alls like in your case. You should make your expectation explicit here, like so:
Mock.ArrangeSet(() => properties.AfterProperties["Title"] = "abc").MustBeCalled();

Even better, you can replace the strict mock with a loose mock. Then, you can use property stubs, instead of arrangements, like so:
// arrange
SPItemEventProperties properties = Mock.Create<SPItemEventProperties>();
properties.BeforeProperties["Title"] = "Old Title";
 
// act
Person p = new Person();
p.DoSomething(properties);     // -> properties.AfterProperties["Title"] = "abc";
   
// assert
Assert.AreEqual("abc", properties.AfterProperties["Title"]);


Regards,
Stefan
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
Gary
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Gary
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or