Set Behaviors in AutoMocking

2 posts, 1 answers
  1. Amir
    Amir avatar
    1 posts
    Member since:
    Jan 2014

    Posted 24 Jan 2014 Link to this post

    How can i set a behavior like strict to a special or all MockContainer's instances?

  2. Answer
    Kaloyan avatar
    878 posts

    Posted 24 Jan 2014 Link to this post

    Hi Amir,

    It is possible to set mock behavior for all MockContainer's instances in the latest version of JustMock. In it, we have dropped the Telerik.JustMock.Container assembly. Instead, the Automock container now resides in the Telerik.JustMock.AutoMock namespace from the Telerik.JustMock.dll assembly. More details can be found here.

    To show you how this is achieved, I have used the first example from the above link. Here is the system under test:
    public interface IFirstDependency
        IList<object> GetList();
    public interface ISecondDependency
        string GetString();
    public class ClassUnderTest
        private IFirstDependency firstDep;
        private ISecondDependency secondDep;
        public ClassUnderTest(IFirstDependency first, ISecondDependency second)
            this.firstDep = first;
            this.secondDep = second;
        public IList<object> CollectionMethod()
            var firstCollection = firstDep.GetList();
            return firstCollection;
        public string StringMethod()
            var secondString = secondDep.GetString();
            return secondString;

    Now, I can execute the following test:
    public void ShouldMockDependenciesWithContainer()
        // Arrange
        // Create settings for the MockingContainer
        var settings = new AutoMockSettings();
        // Set Behavior.Strict.
        settings.MockBehavior = Behavior.Strict;
        var container = new MockingContainer<ClassUnderTest>(settings);
        string expectedString = "Test";
           secondDep => secondDep.GetString()).Returns(expectedString);
        // Act
        // This will pass as the call to secondDep.GetString() is arranged.
        var actualString = container.Instance.StringMethod();
        // This will throw StrictMockException as there are not arranged calls.
        var test = container.Instance.CollectionMethod();
        // Assert
        Assert.AreEqual(expectedString, actualString);
    it will fail because of StrictMockException.

    I hope this helps.

    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
Back to Top