Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / JustMock > General Discussions > How to set up property assignment?

Not answered How to set up property assignment?

Feed from this thread
  • Vagif avatar

    Posted on Aug 2, 2010 (permalink)

    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

    Reply

  • Ricky Ricky admin's avatar

    Posted on Aug 3, 2010 (permalink)

    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

    Reply

  • Vagif avatar

    Posted on Aug 3, 2010 (permalink)

    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.

    Reply

  • Ricky Ricky admin's avatar

    Posted on Aug 3, 2010 (permalink)

    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

    Reply

  • Vagif avatar

    Posted on Aug 3, 2010 (permalink)

    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

    Reply

  • Chris Chris admin's avatar

    Posted on Aug 6, 2010 (permalink)

    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

    Reply

  • Jeff avatar

    Posted on Aug 17, 2011 (permalink)

    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?

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / JustMock > General Discussions > How to set up property assignment?