InMemory database

Running tests against a SQL Server is time-consuming, error-prone, and potentially costly. Establishing a connection to a database takes time, and remember, you want your test suite to be lightning-fast. It might also be a problem to rely on data if the database is used by others, whether in a development environment, by quality assurance engineers, and so on. You certainly wouldn't want to run your integration tests against a production database. Additionally, running tests against a database hosted in the cloud (for example, AWS, Azure, and so on) could potentially incur a dollar cost in terms of bandwidth and processing.

Luckily, it's quite trivial to configure a solution that uses Entity Framework to use an InMemory database.

First, install a NuGet package for the InMemory database.

Microsoft.EntityFrameworkCore.InMemory

Now, modify the test you created before so that the database context is created InMemory:

[Fact]
public void ItExists()
{
var options = new DbContextOptionsBuilder<SpeakerMeetContext>()
.UseInMemoryDatabase("SpeakerMeetInMemory")
.Options;

var context = new SpeakerMeetContext(options);

var repository = new Repository.Repository<Speaker>(context);
}

The test should now pass because the context is now being created InMemory.

Next, create a test to verify that a collection of Speaker entities is returned when the GetAll method is called:

[Fact]
public void GivenSpeakersThenQueryableSpeakersReturned()
{
using (var context = new SpeakerMeetContext(_options))
{
// Arrange
var repository = new Repository.Repository<Speaker>(context);

// Act
var speakers = repository.GetAll();

// Assert
Assert.NotNull(speakers);
Assert.IsAssignableFrom<IQueryable<Speaker>>(speakers);
}
}

Now, turn your attention to the Get method in the repository. Create a new test method to verify that a null Speaker entity is returned when a speaker with the given ID is not found:

[Fact]
public void GivenSpeakerNotFoundThenSpeakerNull()
{
using (var context = new SpeakerMeetContext(_options))
{
// Arrange
var repository = new Repository.Repository<Speaker>(context);

// Act
var speaker = repository.Get(-1);

// Assert
Assert.Null(speaker);
}
}

This should pass right away. Now, create a test to verify that a Speaker entity is returned when a speaker with the supplied ID exists:

[Fact]
public void GivenSpeakerFoundThenSpeakerReturned()
{
using (var context = new SpeakerMeetContext(_options))
{
// Arrange
var repository = new Repository.Repository<Speaker>(context);

// Act
var speaker = repository.Get(1);

// Assert
Assert.NotNull(speaker);
Assert.IsAssignableFrom<Speaker>(speaker);
}
}

This test will not pass quite yet. Regardless of whether or not a Speaker with the ID of 1 exists in your development database, the speakers table in the InMemory database is currently empty. Adding data to the InMemory database is quite simple.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.78.56