How to set value to a local variable of a class using NSubstitute in TestProject?

坚强是说给别人听的谎言 提交于 2020-01-14 05:52:27

问题


I need to mock with NSubstitute and need to set local variable command of a class LoanCreateHandler to mock data with it's parameter Z. I have code like give below:

public class ClassA {
  public string Prop1 { get; set; } 
  public string Prop2 { get; set; } 
  … // Here I have some other properties
}

public class CreateLoanCommand {
  public string X { get; set; } 
  public string Y { get; set; } 
  public ClassA Z { get; set; } 
}    

public class LoanCreateHandler {       

    public Response Handle(LoanCreateRequest request)
    {
        var response = CreateTypedResponse();
        var command = new CreateLoanCommand
        { 
            X = request.X,
            Y = request.Y
        };

        _cqsCommandProcessor.Execute(command); //here I am setting value of command.Z param

        if (command.Z == null)
        {
          //do something
        }else{
          //do another
        }

        return true; // returns response 
    }
}

So here when I want to mock LoanCreateHandler for code coverage. Else loop code is not getting covered. Please find unit test below:

[TestClass]
public class LoanCreateHandlerTests
{
    [TestMethod, TestCategory(Tc.Unit)]
    public void LoanCreateHandler_SuccessTest()
    {
        var loanCreateRequest = new LoanCreateRequest
        { 
            X = "val1",
            Y = "val2"                                    
        };
        var loanCreateResponse = true;


        var createLoanCommand = new CreateLoanCommand()
        {
            X = "val1",
            Y = "val2",
            Z = new ClassA()
            {
                Prop1 = "val1", Prop2 = "val2"…
            }
        };

        _TestHelper.CqsCommandProcessor.Execute(Arg.Any<CreateLoanCommand>());

        var loanCreateHandler = new LoanCreateHandler();
        loanCreateHandler.Handle(loanCreateRequest).Returns(loanCreateResponse);
        //here when call goes to Handle() method it creates new LoanCreateRequest object and I want to replace that object with my LoanCreateRequest object, which is created above.

        Assert.IsNotNull(loanCreateResponse);            
    }
}

回答1:


You should use the When..Do callback to setup void method.

I'm assuming _TestHelper.CqsCommandProcessor returns the same object that LoanCreateHandler._cqsCommandProcessor does.

That said, you should do something like this:

[TestClass]
public class LoanCreateHandlerTests
{
    [TestMethod, TestCategory(Tc.Unit)]
    public void LoanCreateHandler_SuccessTest()
    {
        var z = new ClassA()
        {
            Prop1 = "val1",
            Prop2 = "val2"
        };

        _TestHelper.CqsCommandProcessor
            .When(x => x.Execute(Arg.Any<LoanCreateRequest>()))
            .Do(x => x.Arg<LoanCreateRequest>().Z = z);

        var loanCreateHandler = new LoanCreateHandler();
        var loanCreateRequest = new LoanCreateRequest
        {
            X = "val1",
            Y = "val2"
        };
        var loanCreateResponse = loanCreateHandler.Handle(loanCreateRequest);

        Assert.IsNotNull(loanCreateResponse);
    }
}


来源:https://stackoverflow.com/questions/35131007/how-to-set-value-to-a-local-variable-of-a-class-using-nsubstitute-in-testproject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!