¿Cómo establecer el valor en una variable local de una clase usando NSubstitute en TestProject?

Necesito simularme con NSubstitute y debo configurar el command de variable local de una clase LoanCreateHandler para LoanCreateHandler datos con su parámetro Z Tengo un código como el siguiente:

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

Así que aquí cuando quiero burlar a LoanCreateHandler por la cobertura de código. El código de bucle de otro tipo no se está cubriendo. Por favor encuentre la prueba de unidad a continuación:

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

Debe utilizar el método de callback When..Do para configurar el método de void .

Supongo que _TestHelper.CqsCommandProcessor devuelve el mismo objeto que LoanCreateHandler._cqsCommandProcessor .

Dicho esto, deberías hacer algo como esto:

 [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())) .Do(x => x.Arg().Z = z); var loanCreateHandler = new LoanCreateHandler(); var loanCreateRequest = new LoanCreateRequest { X = "val1", Y = "val2" }; var loanCreateResponse = loanCreateHandler.Handle(loanCreateRequest); Assert.IsNotNull(loanCreateResponse); } }