setting testing value which has been set earlier using .Returns

4 posts, 0 answers
  1. Gary
    Gary avatar
    2 posts
    Member since:
    Dec 2010

    Posted 24 Jun 2015 Link to this post

    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.

     

  2. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 29 Jun 2015 Link to this post

    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
  3. DevCraft R3 2016 release webinar banner
  4. Gary
    Gary avatar
    2 posts
    Member since:
    Dec 2010

    Posted 29 Jun 2015 in reply to Kaloyan Link to this post

    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

  5. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 30 Jun 2015 Link to this post

    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
Back to Top