简体   繁体   English

尝试运行XUnit测试时出错

[英]Error when attempting to run my XUnit Tests

I wrote a few tests in XUnit to test my data access layer. 我在XUnit中编写了一些测试来测试我的数据访问层。 I instantiated my DAL objects & configs the same way I would if I were using it in the actual web application(this is configured to run against a dev environment for testing purposes), however XUnit throws an error: 我实例化DAL对象和配置的方式与在实际的Web应用程序中使用DAL对象和配置的方式相同(此配置用于测试目的是针对开发环境运行),但是XUnit会引发错误:

Message: The following constructor parameters did not have matching fixture data: IConfiguration config, IMediaDataAccess media 消息:以下构造函数参数没有匹配的夹具数据:IConfiguration配置,IMediaDataAccess媒体

I'm a bit new to XUnit, so unsure what the problem is. 我对XUnit有点陌生,所以不确定是什么问题。 Normally ASP.NET would inject instances of IConfiguration and IMediaDataAccess for me, but that doesn't seem to be the case here. 通常,ASP.NET会为我注入IConfiguration和IMediaDataAccess的实例,但是在这里似乎并非如此。

My Test class & a sample test case: 我的测试课程和示例测试用例:

    public class DataAccessLayerTests
    {
        public IConfiguration Config { get; set; }
        private IMediaDataAccess mediaData;

        public IMediaDataAccess MediaData { get => mediaData; set => mediaData = value; }

        public DataAccessLayerTests(IConfiguration config, IMediaDataAccess media)
        {
            this.MediaData = media;
            this.Config = config;
        }

        public void GetAllMediaAsync_MediaListIsReturned()
        {


            List<Media> testData = mediaData.GetAllMedia().Result;


            Assert.IsType<List<Media>>(testData);

        }

}

The test(s) all fail due to the following error: Message: The following constructor parameters did not have matching fixture data: IConfiguration config, IMediaDataAccess media 由于以下错误,所有测试均失败:消息:以下构造函数参数没有匹配的夹具数据:IConfiguration配置,IMediaDataAccess媒体

For anyone else having this problem, Alexey's comment is correct. 对于其他有此问题的人,Alexey的评论是正确的。 You need to download a mocking framework (like Moq) and use it to mock up the dependencies your code is expecting. 您需要下载一个模拟框架(例如Moq),并使用它来模拟代码所期望的依赖关系。 For example, below is one of my fixed unit tests: 例如,以下是我的固定单元测试之一:

public void IndexDataModel_ShouldDisplayMedia()
{

    var mockLogger = new Mock<ILogger<IndexModel>>();
    var mockDataAccess = new Mock<IMediaDataAccess>();
    mockDataAccess.Setup(media => media.GetAllMedia()).ReturnsAsync(GetTestMedia());
    IndexModel indexController = new IndexModel(mockDataAccess.Object, mockLogger.Object);


    var result = indexController.OnGet();
    var viewResult = Assert.IsType<PageResult>(result);
    var model = Assert.IsAssignableFrom<IEnumerable<Media>>(
        indexController.mediaList);

}

The trick is you need to mock up anything that you normally depend on getting injected into your constructor, in my case this was: 诀窍是您需要模拟通常依赖于注入到构造函数中的所有内容,在我的情况下,这是:

var mockLogger = new Mock<ILogger<IndexModel>>();
var mockDataAccess = new Mock<IMediaDataAccess>();

My constructor takes both an ILogger and an IMediaDataAccess, so I needed to mock those. 我的构造函数同时使用ILogger和IMediaDataAccess,因此我需要模拟它们。 Additionally, the other code is there to provide dummy return values when your mocked dependencies are used by the test. 另外,当测试使用模拟的依赖项时,还有其他代码可提供伪返回值。 This is done with the .Setup line of code. 这是通过.Setup代码行完成的。 All this does(I think) is when GetAllMedia() is called, the mock object returns the contents of GetTestMedia() instead of needing to make the actual calls. 所有这些(我认为)是在调用GetAllMedia()时,模拟对象返回GetTestMedia()的内容,而不需要进行实际的调用。 Just make sure whatever function you write has the same return type as the real function. 只要确保您编写的任何函数都具有与真实函数相同的返回类型即可。 For reference, this is my GetTestMedia() function: 供参考,这是我的GetTestMedia()函数:

    private List<Media> GetTestMedia()
    {
        var listMedia = new List<Media>();
        Media testMedia = new Media
        {
            Description = "TestDesc",
            Genre = "TestGenre",
            Name = "TestName",
            Rating = MediaRating.Excellent,
            Type = MediaType.Movie,
            Id = 1
        };

        listMedia.Add(testMedia);

        Media testMedia2 = new Media
        {
            Description = "TestDesc2",
            Genre = "TestGenre2",
            Name = "TestName2",
            Rating = MediaRating.Poor,
            Type = MediaType.Music,
            Id = 2
        };

        listMedia.Add(testMedia2);

        return listMedia;
    }

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

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