单元测试存储库 - 看起来是否合适?

时间:2015-10-16 09:40:46

标签: unit-testing repository

所以,这是我第一次尝试进行单元测试,我只是想知道我是否做得对,因为有点像我自欺欺人,任何sugestions和/或反馈欢迎,谢谢:)

[TestClass]
public class ArticleRepositoryTest
{
    IArticleRepository _articleRepository;

    const string drawingNumber = "drawingNumber";
    const string searchString = "searchString";
    const string articleDescription = "I have a description";
    ArticleDTO articleDTO = new ArticleDTO() { DrawingNumber = drawingNumber };

    [TestInitialize]
    public void Initialize()
    {
        //Arange
        _articleRepository = A.Fake<IArticleRepository>();

        A.CallTo(() => _articleRepository.GetAll()).Returns(new List<ArticleDTO>());
        A.CallTo(() => _articleRepository.GetOne(drawingNumber)).Returns(new ArticleDTO { DrawingNumber = drawingNumber });
        A.CallTo(() => _articleRepository.GetSearch(searchString)).Returns(new List<ArticleDTO>());
        A.CallTo(() => _articleRepository.SaveData(articleDTO)).Invokes(call => { _articleRepository.GetAll().Result.Add(articleDTO); });
        A.CallTo(() => _articleRepository.Edit(articleDTO)).Invokes(call => _articleRepository.GetOne(drawingNumber).Result.ArticleDescr = articleDescription);
    }

    [TestMethod]
    public void Article_GetAll_ReturnsListOfArticleDTO()
    {
        //Arange - Done in the Initialize() method.
        //Act
        var articlesDTO = _articleRepository.GetAll();
        //Assert
        Assert.IsInstanceOfType(articlesDTO, typeof(Task<ICollection<ArticleDTO>>));
    }

    [TestMethod]
    public void Article_GetOneByDrawingNumber_ReturnsArticleDTO()
    {
        //Arange - Done in the Initialize() method.
        //Act
        var articleDTO = _articleRepository.GetOne(drawingNumber);
        //Assert
        Assert.IsInstanceOfType(articleDTO, typeof(Task<ArticleDTO>));
        Assert.AreEqual(drawingNumber, articleDTO.Result.DrawingNumber);
    }

    [TestMethod]
    public void Article_GetSearch_ReturnsListOfArticleDTO()
    {
        //Arange - Done in the Initialize() method.
        //Act
        var articlesDTO = _articleRepository.GetSearch(searchString);
        //Assert
        Assert.IsInstanceOfType(articlesDTO, typeof(Task<ICollection<ArticleDTO>>));
    }

    [TestMethod]
    public async Task Article_SaveData_SavesTheArticleToTheDatabase()
    {
        //Arange - the repository and the articleDTO variable is set up in the Initialize() method.

        //Act
        _articleRepository.SaveData(articleDTO);
        //Assert
        A.CallTo(() => _articleRepository.SaveData(articleDTO)).MustHaveHappened();
        var list = await _articleRepository.GetAll();
        Assert.AreEqual(1, list.Count);
    }

    [TestMethod]
    public void Article_Edit_EditsAnInstanceOfArticle()
    {
        //Arange - the repository and the articleDTO variable is set up in the Initialize() method.

        //Act
        articleDTO.ArticleDescr = articleDescription;
        _articleRepository.Edit(articleDTO);
        var x = _articleRepository.GetOne(drawingNumber).Result.ArticleDescr;
        //Assert
        A.CallTo(() => _articleRepository.Edit(articleDTO)).MustHaveHappened();
        Assert.AreEqual(articleDescription, x);
    }

另外,我是否应该构建一个UnitOfWork类?你能在那里提出一些论点吗?谢谢。

0 个答案:

没有答案