How do I assert that a property has been set in a mocked class?

2 posts, 0 answers
  1. Dustyn
    Dustyn avatar
    2 posts
    Member since:
    Oct 2017

    Posted 31 Dec 2018 Link to this post

    I'm using the MockingContainer to automatically set up my dependencies. How do I assert that a property on one of those dependencies gets set?

    [SetUp]
    public void SetUp()
    {
        //arrange
        _baseUrl = "http://baseUrl";
        _container = new MockingContainer<ApiInteractionService>();
        _container.Arrange<IConfigService>(m => m.BaseUrl).Returns(_baseUrl);
        _uut = _container.Instance;
    }

     

    The following fails with 0 calls, which makes sense since I believe it's looking at the Getter, not the Setter. So how do I assert that the Setter was called by the unit under test?

    [Test]
    public void BaseUrlSet()
    {
        //act
        var _ = _uut.GetMazeId((InitialRequest) Arg.AnyObject);
     
        //assert     
        _container.Assert<IRestService>(m => m.BaseUrl, Occurs.Once());
    }

     


  2. Lyubomir Rusev
    Admin
    Lyubomir Rusev avatar
    2 posts

    Posted 03 Jan Link to this post

    Hi Dustyn,

    The sample code that you provided was very helpful for reproducing the case on our side. You have properly observed that the assertion in your example is actually testing if the property getter was executed once. Since JustMock does not provide straight-forward API for achieving this exact testing scenario, below we will propose two example solutions. Note that in our code snippets we're using the following helper namespaces:

    using Telerik.JustMock.AutoMock.Ninject;
    using Telerik.JustMock.Helpers;
     
    In our samples we will be mocking the following classes:

    public interface IConfigService
    {
        string BaseUrl { get; set; }
    }
    public class ApiInteractionService
    {
        IConfigService srv;
        public ApiInteractionService(IConfigService s)
        {
            srv = s;
        }
        public string Foo()
        {
            return srv.BaseUrl;
        }
    }

    The first example is organized with separate test initialization method similar to the code that you provided. The approach is based on using arrangement that changes the behavior of the property setter. This way it is possible to raise a flag when the property is set and later assert the value of the flag.

    [TestClass]
    public class Example1
    {
        JustMock.AutoMock.MockingContainer<ApiInteractionService> container;
        ApiInteractionService sut;
        bool propertySet;
     
        [TestInitialize]
        public void SetUp()
        {
            container = new JustMock.AutoMock.MockingContainer<ApiInteractionService>();
            propertySet = false;
            container.Get<IConfigService>().ArrangeSet(m => m.BaseUrl = Arg.AnyString).DoInstead(() => propertySet = true);
            sut = container.Instance;
        }
     
        [TestMethod]
        public void TestExample1()
        {
            sut.Foo();
     
            Assert.True(propertySet);
        }
    }

    The second example demonstrates how to use another approach in the arrangement call to test if the setter was called once. In this case the default behavior of the setter is not changed and may be more convenient in some circumstances.

    [TestClass]
    public class Example2
    {
        [TestMethod]
        public void TestExample2()
        {
            var sut = new JustMock.AutoMock.MockingContainer<ApiInteractionService>();
     
            sut.Get<IConfigService>().ArrangeSet(m => m.BaseUrl = Arg.AnyString).OccursOnce();
     
            sut.Instance.Foo();
     
            sut.AssertAll();
        }
    }

    Hope that the provided examples are helpful. Feel free to contact us if you need further assistance.

    Regards,
    Lyubomir Rusev
    Progress 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