Automocking with simple types

2 posts, 0 answers
  1. Russell
    Russell avatar
    76 posts
    Member since:
    Sep 2006

    Posted 14 Nov 2012 Link to this post

    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?
  2. Kaloyan
    Kaloyan avatar
    880 posts

    Posted 15 Nov 2012 Link to this post

    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:

     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:  
    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:
    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,
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top