简体   繁体   中英

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

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);            
    }
}

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM