简体   繁体   English

我想编写一个测试,该测试肯定会告诉您是否调用了模拟方法

[英]I want to write a test that will definitely tell if the mock-method was called

I have a method: 我有一个方法:

public class CreateNewAccountUseCase
{
    private readonly IAccountRepository _accountRepository;

    public CreateNewAccountUseCase(IAccountRepository accountRepository)
    {
        _accountRepository = accountRepository;
    }

    public virtual async Task Execute(Account account)
    {
       await _accountRepository.Create(account);
    }
}

I use it in the controller: 我在控制器中使用它:

public async Task<ActionResult<AccountModel>>  CreateAccount([FromBody]CreatingAccount creatingAccount)
{
    var account = creatingAccount.ConvertToAccount();

    await _createNewAccountUseCase.Execute(account);

    return Created($"/accounts/{account.Id}", account);
}

I want to be sure that the UseCase method will be exactly called. 我想确保UseCase方法将被正确调用。

I wrote this controller test: 我写了这个控制器测试:

[Fact]
public async Task Create()
{
    // arrange
    var account = new CreatingAccount();

    var accountRepository = new Mock<IAccountRepository>();
    var createNewAccountUseCase = new Mock<CreateNewAccountUseCase>(accountRepository.Object);

    createNewAccountUseCase.Setup(m => m.Execute(account)).Returns(Task.CompletedTask);

    // act
    await controller.CreateAccount(account);
    // assert
    createNewAccountUseCase.Verify(m => m.Execute(account), Times.Once);
}

I expect createNewAccountUseCase.Verify() to check if the method has been method but I get failed test with a message: 我期望createNewAccountUseCase.Verify()检查该方法是否为方法,但测试失败并显示以下消息:

Moq.MockException : 
Expected invocation on the mock once, but was 0 times: m => m.Execute(account)

Performed invocations:
Mock<CreateNewAccountUseCase:1> (m):
No invocations performed.

How do I make sure the method (createNewAccountUseCase.Verify()) is called? 如何确保方法(createNewAccountUseCase.Verify())被调用?

You are mocking the subject under test. 您正在嘲笑被测对象。 Instead mock the dependency and invoked the method under test 而是模拟依赖项并调用测试中的方法

[Fact]
public async Task Create() {
    // arrange
    var account = new CreatingAccount();

    var accountRepository = new Mock<IAccountRepository>();
    accountRepository.Setup(m => m.Create(It.IsAny<Account>())).Returns(Task.CompletedTask);

    var createNewAccountUseCase = new CreateNewAccountUseCase(accountRepository.Object);

    // act
    await createNewAccountUseCase.Execute(account.ConvertToAccount());

    // assert
    accountRepository.Verify(m => m.Create(It.IsAny<Account>()), Times.Once);
}

The assertion can then be done on the repository mock to verify that the method behaves as expected. 然后可以在存储库模拟中进行断言,以验证该方法的行为是否符合预期。

In the controller test of your question you could have used the mock use case but your also need to make sure that it was injected into the controller. 在您的问题的控制器测试中,您可能已经使用了模拟用例,但还需要确保已将其注入到控制器中。

But your example showed no such injection. 但是您的示例未显示此类注入。

The following example creates the controller and explicitly injects the dependency 以下示例创建控制器并显式注入依赖项

[Fact]
public async Task Create() {
    // arrange
    var account = new CreatingAccount();

    var accountRepository = new Mock<IAccountRepository>();
    accountRepository.Setup(m => m.Create(It.IsAny<Account>())).Returns(Task.CompletedTask);
    var createNewAccountUseCase = new CreateNewAccountUseCase(accountRepository.Object);

    var controller = new MyController(createNewAccountUseCase);

    // act
    await controller.CreateAccount(account);

    // assert
    accountRepository.Verify(m => m.Create(It.IsAny<Account>()), Times.Once);
}

暂无
暂无

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

相关问题 如何使用Rhino Mock测试是否调用了类的私有方法? - How can I test if a private method of a class is called or not with rhino mock? 如何为这个在循环中调用的方法编写单元测试? - How do I write a unit test for this method that is called inside a loop? 如何在我想要使用NUnit测试的方法中模拟方法调用? - How do I mock a method call inside the method I want to test using NUnit? 在模拟对象上调用预期方法后,如何立即传递单元测试? - How can I pass a unit test immediately after an expected method is called on the mock object? 如何在我要测试的方法中模拟 Process.GetProcessesByName() 调用? - How to mock away Process.GetProcessesByName() call in a method I want to test? 如何为ExecuteNonQuery和ExecuteScalar和GetDataSet方法编写模拟单元测试用例 - How to write mock unit test case for ExecuteNonQuery & ExecuteScalar & GetDataSet method 我想检查该项目是否在数组/列表中,如果不是,则将其写下来,然后告诉他们它不在那里 - I want to check if the item is in the array/list and if it is then write that down if not then tell them it is not in there 如何为 void 方法编写单元测试并创建模拟数据集和数据行(数据集和数据行都是强类型)? - How do I write a unit test for a void method and create a mock dataset and a datarow(Both dataset and datarow are strongly typed)? 我怎么能告诉哪个对象叫做方法? - How can I tell what object called a method? 模拟测试方法 - Mock a method for test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM