Xunit项目的依赖注入
我正在研究ASP.NET Core MVC Web应用程序。
我的解决方案包含2个项目:
- 一个用于应用程序的项目和
- 第二个项目,专用于单位测试(XUNIT)。
我在测试项目中添加了对应用程序项目的参考。
我现在想做的是在Xunit测试项目中编写一类,该项目将通过实体框架与数据库进行通信。
我在应用程序项目中正在做的是通过构造依赖依赖注入访问我的dbContext
类。
但是我无法在测试项目中这样做,因为我没有startup.cs
文件。在此文件中,我可以声明将有哪些服务可用。
那么,我该怎么做才能在测试类中获取对我的dbContext
实例的引用?
I am working on an ASP.Net Core MVC Web application.
My Solution contains 2 projects:
- One for the application and
- A second project, dedicated to unit tests (XUnit).
I have added a reference to the application project in the Tests project.
What I want to do now is to write a class in the XUnit Tests project which will communicate with the database through entity framework.
What I was doing in my application project was to access to my DbContext
class through constructor dependency injection.
But I cannot do this in my tests project, because I have no Startup.cs
file. In this file I can declare which services will be available.
So what can I do to get a reference to an instance of my DbContext
in the test class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以实现自己的服务提供商来解决
dbContext
。但是,在单元测试中使用EF在您的脑海中却不是一个好主意,最好嘲笑dbcontext。
良好单位测试的解剖结构
You can implement your own service provider to resolve
DbContext
.But bear in your mind using EF inside a unit test is not a good idea and it's better to mock DbContext.
The Anatomy of Good Unit Testing
您可以使用
You can use Xunit.DependencyInjection
对于单元测试,您需要嘲笑您的上下文。
有一个很棒的nuget软件包用于嘲笑,称为签名。
有一些帮助使您开始的帮助:
对于集成测试,您可以执行同样的事情,但是设置是:
确保您的
testClass
从iDisposable
(testClass:idisposable:idisposable < /code>),以便您可以在每个测试之后处置上下文。
https://xunit.github.io/docs/docs/docs/shared-context
For unit tests you need to mock your context.
There is a great nuget package for mocking that is called Moq.
Some help to get you started:
For integration tests you do the same thing but the setup is:
Make sure that your
TestClass
inherits fromIDisposable
(TestClass : IDisposable
) so that you can dispose the context after each test.https://xunit.github.io/docs/shared-context
您可以使用Microsoft.entityFrameworkCore.inmemory
DbContextOptionsBuilder&lt; dbContext&gt;()
。
var _dbContextOptions = new = new DbContext(_DBContextOptions);
You can to use package Microsoft.EntityFrameworkCore.InMemory
var _dbContextOptions = new DbContextOptionsBuilder<DbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options;
And then
var context = new DbContext(_dbContextOptions);