Search code examples
c#justmock

How to fake arg constructor JustMock


I don't know how to fake constructor has arg

Mock.SetupStatic(typeof(A), StaticConstructor.Mocked);

with Class A has Constructor arg s:

public A(string s)
{}

Please help me! Thanks.


Solution

  • SetupStatic is meant to be used with static constructors. In order to mock an instance constructor with arguments you can use Mock.Arrange like this:

            var expected = "StringArg";
            string arg = null;
            Mock.Arrange(() => new A(Arg.AnyString)).DoInstead<string>(x => arg = x);
    
            new A(expected);
    
            Assert.Equal(expected, arg);