This is a migrated thread and some comments may be shown as answers.

Set Behaviors in AutoMocking

1 Answer 36 Views
JustMock Free Edition
This is a migrated thread and some comments may be shown as answers.
Amir
Top achievements
Rank 1
Amir asked on 24 Jan 2014, 09:18 AM
How can i set a behavior like strict to a special or all MockContainer's instances?

1 Answer, 1 is accepted

Sort by
0
Accepted
Kaloyan
Telerik team
answered on 24 Jan 2014, 04:47 PM
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:
[TestMethod]
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";
 
    container.Arrange<ISecondDependency>(
       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.

Regards,
Kaloyan
Telerik
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.
Tags
JustMock Free Edition
Asked by
Amir
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Share this question
or