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?

    public void SetUp()
        _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?

    public void BaseUrlSet()
        var _ = _uut.GetMazeId((InitialRequest) Arg.AnyObject);
        _container.Assert<IRestService>(m => m.BaseUrl, Occurs.Once());


  2. Lyubomir Rusev
    Lyubomir Rusev avatar
    4 posts

    Posted 03 Jan 2019 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.

    public class Example1
        JustMock.AutoMock.MockingContainer<ApiInteractionService> container;
        ApiInteractionService sut;
        bool propertySet;
        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;
        public void TestExample1()

    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.

    public class Example2
        public void TestExample2()
            var sut = new JustMock.AutoMock.MockingContainer<ApiInteractionService>();
            sut.Get<IConfigService>().ArrangeSet(m => m.BaseUrl = Arg.AnyString).OccursOnce();

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

    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