check Property never called

2 posts, 0 answers
  1. Ryan
    Ryan avatar
    2 posts
    Member since:
    Mar 2011

    Posted 22 Mar 2011 Link to this post

    How can I arrange the mock to assert that a property setter is never called?

    I've tried the following: 
    Mock.ArrangeSet(() => mockVirtualEntity.EffectiveFrom = DateTime.Now).IgnoreArguments().OccursNever();

    I dont care what the value being set is - I just want to make sure the property setter is never called?

  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 25 Mar 2011 Link to this post

    Hi Ryan,

    Thanks again for your question. Yes it is possible to assert property set for any values. Below is a test that shows how you can arrange property set with Arg.IsAny<T>():

    [TestMethod, ExpectedException(typeof(MockAssertionException))]
     public void ShouldThowForOccursNeverForAnyValue()
     {
         var foo = Mock.Create<IFoo>();
         Mock.ArrangeSet(() => foo.EffectiveFrom = Arg.IsAny<DateTime>()).OccursNever();
         foo.EffectiveFrom = DateTime.Now;
         Mock.Assert(foo);
     }


    Similarly, it is also possible to assert the specific property for above setup in the following way:

    Mock.AssertSet(() => foo.EffectiveFrom = Arg.IsAny<DateTime>());

    Moreover, you can also write the first test in this way:

    [TestMethod, ExpectedException(typeof(MockAssertionException))]
    public void ShouldThowForOccursNeverForAnyValueWithIgnoreArguments()
    {
        var foo = Mock.Create<IFoo>();
        Mock.ArrangeSet(() => foo.EffectiveFrom = DateTime.Now).IgnoreArguments().OccursNever();
        foo.EffectiveFrom = DateTime.Now;
        Mock.Assert(foo);
    }

    Finally, i have attached the test project to let you have a look. Please do feel free to write us back for any other issues.

    Kind Regards,
    Ricky
    the Telerik team
  3. DevCraft R3 2016 release webinar banner
Back to Top