Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

c# - Are there any ways to call database data to provide in Unit Testing

This is the Test that i have:

[Test]
public async Task Late_Test()
{
  var options = new DBContextOptionsBuilder<MyContext>()
  .Options;
  
  using(var context = MyContext(options))
  {
    //Act?
  }
    // Assert?
}

This is the service that I am trying to create a test for:

public async Task<int> LatesInMin(Model1 model1, bool isbool) 
{
 //code here
}

The code int the service also needs the data in the foreign keys attached to Model1. Can i call a data from the database and use it as the parameter when i call the service? Or do i have to manually provide that data as well? I tried and using linq causes an error and I searched that you cant use extension methods such as 'Where()' and 'FirstOrDefault()'


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For your tests to be isolated and predictable, you need to make sure that you set up and tear down anything associated with the tests. Therefore, you should be seeding a data source either with data alongside your test code, or with data in an associated file which is loaded in before each test. What I'm basically trying to get at is that I think you should pull all of the data you require out of the database, save that into a file somewhere, and then load it in before you run each test. By "load", you could spin up a real database and save the values, or you could use the InMemoryDatabase feature provided by Entity Framework.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...