Skip to content

Instantly share code, notes, and snippets.

@safestudent
Created December 4, 2018 12:17
Show Gist options
  • Save safestudent/dc137a33d11371e412808388e6c62311 to your computer and use it in GitHub Desktop.
Save safestudent/dc137a33d11371e412808388e6c62311 to your computer and use it in GitHub Desktop.
SprintTrackerWebContextTest.cs
using Microsoft.EntityFrameworkCore;
using SprintTracker.Web;
using SprintTracker.Web.Models;
using System.Linq;
using Xunit;
namespace SprintTracker.Tests
{
public class SprintTrackerWebContextTest
{
/**
* In this test, we're confirming that a sprint object can be
* saved to the database using our context.
**/
[Fact]
public void Add_writes_to_database()
{
// ARRANGE - set up 'in memory' database
var options = new DbContextOptionsBuilder<SprintTrackerWebContext>()
.UseInMemoryDatabase(databaseName: "Add_writes_to_database")
.Options;
// Create Test Sprint
Sprint sprint = new Sprint
{
ID = 1,
SprintName = "Badger",
PredictedPoints = 4,
ActualPoints = 5,
};
// ACT: Populate Database
// The `using` command creates a single instance of the context that is then
// discarded at the end of the command.
using (var context = new SprintTrackerWebContext(options))
{
context.Sprint.Add(sprint);
context.SaveChanges();
}
// ASSERT: data was saved to database
// This 'using' command then creates a completely new instance of the context, so
// we can be sure that the data we're testing is coming from the database, rather
// than just being stored in the context.
using (var context = new SprintTrackerWebContext(options))
{
Assert.Equal(1, context.Sprint.Count());
Assert.Equal(sprint.SprintName, context.Sprint.Single().SprintName);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment