本文介绍了最小起订量,如何测试保存方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个save()方法,不太确定如何测试.下面是我的代码.

I have a save() method, not really sure how to test. below is my code.

public interface IRepository<T>
{
    T Get(int id);
    void Save(T item);
    void Delete(int id);
}

save方法不会返回任何值,我无法比较该值.但是,我已经有4个用户,在添加另一个用户之后,我只检查用户总数,是否足以进行测试?

save method doesn't return any values back, I cannot compare the value. however, I already have 4 users, after adding another one, I only check the total number of users, is it enough to test it?

[Test]
public void Add_a_new_smoothie_user_should_return_total_5_users()
{
    // Arrange

    var totalUsers = _users.Count();

    _mockUserRepository.Setup(s => s.Save(It.IsAny<User>()))
        .Callback((User user) => _users.Add(user));

    var newUser = new User
                      {
                          Id = 3,
                          Email = "[email protected]",
                          Password = "1234567".Hash(),
                          Firstname = "",
                          Lastname = "",

                          CreatedDate = DateTime.Now,
                          LastLogin = DateTime.Now,

                          AccountType = AccountType.Smoothie,
                          DisplayName = "",
                          Avatar = "",
                          ThirdPartyId = "",
                          Status = Status.Approved,
                          Ip = "127.0.0.1"
                      };

    // Act

    _mockUserRepository.Object.Save(newUser);

    // Assert

    Assert.AreEqual(5, _users.Count());
    Assert.AreEqual(1, _users.Count() - totalUsers);
}

推荐答案

您正在嘲笑要测试的功能.这些测试不会证明任何内容,除了您持有用户的数据类型的Add()方法之外.最后,如果您的存储库正在运行,它不会给出任何提示.

You are mocking the part of the functionality you are trying to test. These tests will prove nothing, any other than Add() method of the data type you are holding the users. In the end it doesn't give any ideas if your repository is working.

您应该尝试实现数据库沙箱,以测试存储库功能.

You should try to implement a Database Sandbox for testing your repository functionality.

这篇关于最小起订量,如何测试保存方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 05:57