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

Automocking with simple types

1 Answer 48 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Software
Top achievements
Rank 1
Software asked on 14 Nov 2012, 05:49 PM
I am trying to use automocking to test a class that looks something like this:

class X
{
public X(int code, IFoo a, IBar b, IBaz c, ...) { ... } 
}

Using automocking how do I go about getting the int into the class under test?

1 Answer, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 15 Nov 2012, 01:24 PM
Hello Eric,

 Thank you for contacting Telerik support.

 In order to help you further, I must object that the Automocking container of JustMock is designed to mock non elevated external dependencies(mostly Interfaces). However, we have planned the using of profiler in it for future releases. I would recommend you to check our help documentation about this feature:
 - http://www.telerik.com/help/justmock/basic-usage-automocking.html

 Noting the above, I must say that, with automocking it is not possible to pass a value type along with the external dependencies(the interfaces) into the constructor. Let`s assume we have the following code to be tested:
public interface IFoo
{
    int MyFooProp { get; set; }
}
 
public interface IBar
{
    string MyBarProp { get; set; }
}
 
public interface IBaz
{
    bool MyBazProp { get; set; }
}
 
public class X
{
    public int SampleInt { get; set; }
    public int IntegerValue { get; set; }
    public string StringValue { get; set; }
    public bool BoolValue { get; set; }
 
    public X(int code, IFoo a, IBar b, IBaz c)
    {
        this.SampleInt = code;
        this.IntegerValue = a.MyFooProp;
        this.StringValue = b.MyBarProp;
        this.BoolValue = c.MyBazProp;
    }
}

 Using the automocking feature will make the "code" variable to be passed with it`s default value which is "0" and the following test will behave as expected:  
[TestMethod]
public void TestMethodUsingAutomocking()
{
    var container = new MockingContainer<X>();
 
    container.Arrange<IFoo>((foo) => foo.MyFooProp).IgnoreInstance().Returns(123);
    container.Arrange<IBar>((foo) => foo.MyBarProp).IgnoreInstance().Returns("test");
    container.Arrange<IBaz>((foo) => foo.MyBazProp).IgnoreInstance().Returns(true);
 
    Assert.AreEqual(123, container.Instance.IntegerValue);
    Assert.AreEqual("test", container.Instance.StringValue);
    Assert.AreEqual(true, container.Instance.BoolValue);
    Assert.AreEqual(0, container.Instance.SampleInt);
}

 However, if you need to instantiate your "X" class with concrete value of the "code" variable, I would recommend you to do it the following way:
[TestMethod]
public void TestMethodWithoutAutomocking()
{
    var foo = Mock.Create<IFoo>();
    var bar = Mock.Create<IBar>();
    var baz = Mock.Create<IBaz>();
 
    Mock.Arrange(() => foo.MyFooProp).IgnoreInstance().Returns(123);
    Mock.Arrange(() => bar.MyBarProp).IgnoreInstance().Returns("test");
    Mock.Arrange(() => baz.MyBazProp).IgnoreInstance().Returns(true);
 
    X xInstance = new X(444, foo, bar, baz);
 
    Assert.AreEqual(123, xInstance.IntegerValue);
    Assert.AreEqual("test", xInstance.StringValue);
    Assert.AreEqual(true, xInstance.BoolValue);
    Assert.AreEqual(444, xInstance.SampleInt);
}
 
 I hope this answers your question. Please do not hesitate to contact us again if there is more you wish to know.

Kind regards,
Kaloyan
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
General Discussions
Asked by
Software
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Share this question
or