简体   繁体   English

将模拟的依赖项注入到ViewModel中,而单元测试的行为不符合预期

[英]Injected mocked dependency into ViewModel while unit testing not behaving as expected

I'm trying to do unit testing in view model by using Moq and dependency injection 我正在尝试通过Moq和依赖注入在视图模型中进行单元测试

Basically, I have model: Class PersonModel containing properties FirstName , LastName , Input , Output - no logic or any methods in here. 基本上,我有模型:类PersonModel含有属性FirstNameLastNameInputOutput -没有逻辑或任何在这里的方法。

In my ViewModel , I have: 在我的ViewModel ,我有:

public class ViewModel
{
    private IGetService getService;
    private Personmodel personModel;
    private string result;

    public ViewModel(IGetService getService)
    {
        personModel= new Personmodel ();
        this.getService = getService;
    }

    public string FullName
    {
        get { return personModel.FirstName + " " + personModel.LastName; }
    }
    public string Result
    {
        get { return result; }
        set { result = value; }
    }

    public string FirstName
    {
        get { return personModel.FirstName; }
        set { personModel.FirstName = value; }
    }

    public string LastName
    {
        get { return personModel.LastName; }
        set { personModel.LastName = value; }
    }

    public string Input
    {
        get { return personModel.Input; }
        set { personModel.Input= value; }
    }

    public string Output
    {
        get { return personModel.Output; }
        set { personModel.Output= value; }
    }

    public string Result
    {
        get { return result; }
        set { result = value; }
    }        
    public void ResultFromModelToView()
    {
        getService.GetOutput(personModel);
        Result = string.Join(",", FullName,personModel.Output);            
    }
}

For IGetService , 对于IGetService

public interface IGetService
{
    void GetOutput(PersonModel personModel);
}   

The implementation of GetService is pretty much getting personModel.Outout value depending on business logic GetService的实现几乎是根据业务逻辑获取personModel.Outout值的

Here is my ViewModelTests 这是我的ViewModelTests

public class ViewModelTests
{

    private Mock<IGetService> GetServiceMock{ get; set; }
    private ViewModel ViewModel { get; set; }

    [TestInitialize()]
    public void Initialize()
    {
        GetServiceMock= new Mock<IGetService>();
        ViewModel = new ViewModel (GetServiceMock.Object);
    }

    [TestMethod()]
    public void GetResultTests()
    {
        // Arrange
        GetServiceMock.Setup(x =>  x.GetOutput(It.Is<PersonModel>(
            p=> p.FirstName == "FirstName" &&
                    p.LastName == "LastName" &&
                    p.Input == "Input"
            )));

        // Act
        ViewModel.ResultFromModelToView();

        // Assert
        Assert.AreEqual(ViewModel.Result, "FirstName,LastName,Output");

    }
}

When I run the test I'm getting 当我运行测试时,

Assert failed, with expected result of ",," 断言失败,预期结果为“ ,,”

What have I missed? 我错过了什么?

Given the design of the view model, you were not assigning values to the model that would allow the test to be exercised to completion based on how the mock was set up. 给定视图模型的设计,您没有为模型分配值,该值将不允许根据模拟的设置来完成测试。 You would also need to have the mock mimic the expected behavior of setting the output on the model when GetOutput was invoked. 您还需要使模拟模仿调用GetOutput时在模型上设置输出的预期行为。

The expectation in the assertion is also wrong because ViewModel.FullName is defined as 断言中的期望也是错误的,因为ViewModel.FullName被定义为

public string FullName {
    get { return personModel.FirstName + " " + personModel.LastName; }
}

Note the space between the names and not the comma of the expected result "FirstName,LastName,Output" . 请注意名称之间的空格,而不是预期结果"FirstName,LastName,Output"的逗号。 Which would cause the test to fail even if you provided the necessary values to the view model. 即使您向视图模型提供了必要的值,这也会导致测试失败。

Review the following changes made to the test based on the original example provided 根据提供的原始示例,检查对测试所做的以下更改

[TestClass]
public class ViewModelTests {

    private Mock<IGetService> GetServiceMock{ get; set; }
    private ViewModel ViewModel { get; set; }

    [TestInitialize()]
    public void Initialize()
    {
        GetServiceMock= new Mock<IGetService>();
        ViewModel = new ViewModel (GetServiceMock.Object);
    }

    [TestMethod()]
    public void GetResultTests() {
        // Arrange
        var expectedFirstName = "FirstName";
        var expectedLastName = "LastName";
        var expectedInput = "Input";
        var expectedOutput = "Output";
        var expectedResult = "FirstName LastName,Output";

        //set properties on view model
        ViewModel.FirstName = expectedFirstName;
        ViewModel.LastName = expectedLastName;
        ViewModel.Input = expectedInput;

        //Arrange mock to expect specific model
        GetServiceMock
            .Setup(_ => _.GetOutput(It.Is<PersonModel>(p=> 
                p.FirstName == expectedFirstName &&
                p.LastName == expectedLastName &&
                p.Input == expectedInput
            )))
            .Callback((PersonModel p) => { // <-- use callback to set Output on model
                p.Output = expectedOutput;
            });

        // Act
        ViewModel.ResultFromModelToView();
        actualResult = ViewModel.Result;

        // Assert
        Assert.AreEqual(expectedResult, actualResult);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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