質問

I am using JustMock to aid in unit test writing and have come to something that I thought would be fairly simple but can't find the answer to. I can't post any of the code but I'll try to explain best I can. I came up with an example to show what I'm trying to do. I want to test Class1's Close method, without calling Class2's CloseConnection method. I've tried using Mock.Arrange on an instance of Class2's CloseConnection but I believe it's specific to what instance you do the arrange on. After I setup the unit test and mock (mock code below sample code) then debug inside of the Close method of Class1 it still calls the CloseConnection method from Class2 I think because the Class2 dependency was created inside Class1's constructor and I don't have access to it directly.

public class Class1
{
    private Class2 instanceOfClass2;

    public Class1()
    {
       instanceOfClass2 = new Class2();
    }

    public void Close()
    {
        // other code here
        instanceOfClass2.CloseConnection(true);
    }
}   

public class Class2
{
    public void CloseConnection(bool persist)
    {
        // Method I don't want executed
    }
}

Anyone know how I can test Class1's Close method and ignore the call to Class2's CloseConnection method using JustMock?

I have tried:

Mock.Arrange(() => instanceOfClass2.CloseConnection(persist)).DoNothing();

along with mocked and nonmocked class2 instances and different args (ie Arg.IsAny()) but still cannot prevent this method call from happening.

Refactoring the code is not an option for me, so I can't change it to have the dependencies injected, I read up about constructor mocking with justmock but it seems to ignore the constructor instead of letting me write a different constructor for Class1 to use so that I have access to Class2.

Any ideas?

update 1: Telerik came back to me and said to try using .IgnoreInstance() which still did not prevent the call to CloseConnection. Waiting on another response from Telerik.

役に立ちましたか?

解決

(Full disclosure: I work at Telerik)

Using the latest internal build of JustMock I tried the following test and, with the profiler enabled, it passed successfully. Note that you can't prevent a call to a method, you can only prevent the execution of a method's original body.

    [TestMethod]
    public void ShouldArrangeFutureCallToMethod()
    {
        // arrange CloseConnection for all future instances of Class2.
        // "new X()" is equivalent to using .IgnoreInstance()
        Mock.Arrange(() => new Class2().CloseConnection(Arg.AnyBool)).DoNothing();

        // act
        var c1 = new Class1();
        c1.Close();
        //success: didn't throw
    }

    public class Class1
    {
        private Class2 instanceOfClass2;

        public Class1()
        {
            instanceOfClass2 = new Class2();
        }

        public void Close()
        {
            // other code here
            instanceOfClass2.CloseConnection(true);
        }
    }

    public class Class2
    {
        public void CloseConnection(bool persist)
        {
            throw new NotImplementedException();
        }
    }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top