How to set up property assignment?

7 posts, 0 answers
  1. Vagif
    Vagif avatar
    4 posts
    Member since:
    Jul 2010

    Posted 02 Aug 2010 Link to this post

    Hello,

    In Moq it's easy to set up property assignment behavior, for example:

    Snippet
                var monkey = new Mock<IMonkey>();
                monkey.SetupAllProperties();
                monkey.Object.Name = "Spike";
                Assert.AreEqual("Spike", monkey.Object.Name);

    In Typemock it's not even necessary to call an analog of SetupAllProperties:

    Snippet
                var monkey = Isolate.Fake.Instance<IMonkey>();
                monkey.Name = "Spike";
                Assert.AreEqual("Spike", monkey.Name);

    With JustMock I can't find any method that would assign a property value on a mocked instance. The following code fails in assertion:

    Snippet
                var monkey = Mock.Create<IMonkey>();
                Mock.ArrangeSet(() => { monkey.Name = "Spike"; });
                monkey.Name = "Spike";             Assert.AreEqual("Spike", monkey.Name); // FAILS!

    How do I need to configure property assignment?

    Thanks in advance
  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 03 Aug 2010 Link to this post

    Hello Vagif,

    Thanks for asking such question. Here is how to setup property assignment using JustMock:

    var monkey = Mock.Create<IMonkey>(Behavior.CallOriginal);
    monkey.Name = "Spike";
    Assert.AreEqual(monkey.Name, "Spike");

    Now , Behavior.CallOriginal works in a way that if you dont setup any expectations using Mock.Arrange , by default it will invoke  the original setter or getter .

    Let me know if that is helpful.

    Additionally, in extreme case if you are calling any data source during property get / set [Extremely bad practice], the above solution will fail. Therefore, I will add an backing mock field for mocked properties so that you can also do the following , when you are not setting any expectation through Mock.Arrange:

    var monkey = Mock.Create<IMonkey>(); 
        
    // has a default backing field 
    monkey.Name = "kuddus"
        
    Assert.AreEqual(monkey.Name, "kuddus");

    This is a tiny change , i will hopefually  patch it today so that it comes with upcoming build.


    P.S: While I am editing this ticket, you might have got some extra notifications. Sorry for the inconvenience.


    With Regards,
    the telerik Team

  3. DevCraft R3 2016 release webinar banner
  4. Vagif
    Vagif avatar
    4 posts
    Member since:
    Jul 2010

    Posted 03 Aug 2010 Link to this post

    Nope! Doesn't work this way. Here's the code:

                var monkey = Mock.Create<IMonkey>(Behavior.CallOriginal);
                monkey.Name = "Spike";
                Assert.AreEqual("Spike", monkey.Name); // FAILS!!!

    IMonkey is an interface, there is no original implementation of it. Here's the message I get:

    Test 'Zoo.Tests.JustMockTests.Properties' failed:
      Expected: "Spike"
      But was:  null
        JustMockTests.cs(35,0): at Zoo.Tests.JustMockTests.Properties()

    In other words, with Moq, RhinoMocks and others I don't need to back up properties with any implementation: I configure mocked object to accept any property assignment and in case I retrieve the property value simply return the value I set. I haven't found a way of doing this in JustMock.
  5. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 03 Aug 2010 Link to this post

    Hi Vagif,

    Thanks again for the reply. Yes you are correct at this, this can only be done for class members. Therefore, expect an update in the coming build so that you can do the following:

    var monkey = Mock.Create<IMonkey>();  
          
    // has a default backing field  
    monkey.Name = "Spike";  
          
    Assert.AreEqual(monkey.Name, "Spike");

    Should you need a fixed build earlier, please open a separate support ticket and we'll send a patched version.
    Sorry for the inconvenience.

    Kind regards,
    the Telerik team

    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 Public Issue Tracking system and vote to affect the priority of the items
  6. Vagif
    Vagif avatar
    4 posts
    Member since:
    Jul 2010

    Posted 03 Aug 2010 Link to this post

    Thank you for the feedback.

    I don't need an urgent fix for this. I am currently comparing different mock frameworks, and when comparing property management I noticed that JustMock was not as convenient as other frameworks that had auto-assignment feature built-in. I can wait for a regular release, hopefully it will not take long time.

    Vagif
  7. Chris
    Admin
    Chris avatar
    221 posts

    Posted 06 Aug 2010 Link to this post

    Hi Vagif,
    The build will be available next week. Thanks for your patience.

    All the best,
    Chris
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
  8. Jeff
    Jeff avatar
    4 posts
    Member since:
    Dec 2007

    Posted 17 Aug 2011 Link to this post

    I saw that this issue was resolved in the public issue tracker, but is not scheduled until the next release in the fall.  Is an internal build available to download so that I can test out this functionality?
Back to Top
DevCraft R3 2016 release webinar banner