Skip to content

Instantly share code, notes, and snippets.

@jeroenheijmans
Last active May 5, 2017 07:11
Show Gist options
  • Save jeroenheijmans/5dd7cb39c419be9f7c09d2d698fb844b to your computer and use it in GitHub Desktop.
Save jeroenheijmans/5dd7cb39c419be9f7c09d2d698fb844b to your computer and use it in GitHub Desktop.
Entity Framework minimal integration tests setup
/*
* Create a new Class Library and copy/paste this file over the class1.cs file.
*
* On Package Mananager Console:
*
* @("MSTest.TestFramework","MSTest.TestAdapter","EntityFramework") | foreach { Install-Package $_ }
*
* Run `sqllocaldb c soquestion` to create the instance.
* Afterwards create database `TestDb` e.g. with SSMS on that instance.
*/
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Data.SqlClient;
using System.Linq;
namespace EntityFrameworkIntegrationTests {
public class Person {
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public Pet Pet { get; set; }
}
public class Pet {
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
}
public class MyDbContext : DbContext {
public MyDbContext(string nameOrConnectionString)
: base(nameOrConnectionString) {
Database.SetInitializer<MyDbContext>(null);
}
public virtual DbSet<Person> People { get; set; }
public virtual DbSet<Pet> Pets { get; set; }
}
public class UnitUnderTest {
private string connectionString;
public UnitUnderTest(string connectionString) {
this.connectionString = connectionString;
}
public Person CreateAndSaveNewPersonWithNewPet(string name, string petName) {
using (var context = new MyDbContext(connectionString)) {
var person = context.People.Add(new Person {
Name = "John",
Pet = context.Pets.Add(new Pet { Name = "Fluffy" })
});
context.SaveChanges();
return person;
}
}
}
[TestClass]
public class Tests {
const string connectionString = @"Data Source=(LocalDb)\soquestion;Integrated Security=True;Initial Catalog=TestDb";
[AssemblyInitialize]
public static void AssemblyInitialize(TestContext context) {
ExecuteNonQuery(@"
IF OBJECT_ID('People') IS NOT NULL EXEC('DROP TABLE People;');
IF OBJECT_ID('Pets') IS NOT NULL EXEC('DROP TABLE Pets;');
CREATE TABLE Pets (
Id INT IDENTITY(1,1) PRIMARY KEY,
Name NVARCHAR(250)
);
CREATE TABLE People (
Id INT IDENTITY(1,1) PRIMARY KEY,
Name NVARCHAR(250),
Pet_Id INT,
CONSTRAINT FK_Person_Pet FOREIGN KEY (Pet_Id) REFERENCES Pets(Id)
);
");
}
[TestInitialize]
public void SetUp() {
ExecuteNonQuery(@"DELETE FROM People;");
ExecuteNonQuery(@"DELETE FROM Pets;");
}
[TestMethod]
public void Can_persist_person_with_pet() {
var sut = new UnitUnderTest(connectionString);
var savedPersonId = sut.CreateAndSaveNewPersonWithNewPet("John", "Fluffy").Id;
using (var context = new MyDbContext(connectionString)) {
var person = context.People
.Include(p => p.Pet)
.Single(p => p.Id == savedPersonId);
Assert.AreEqual(person.Name, "John");
Assert.AreEqual(person.Pet.Name, "Fluffy");
}
}
private static void ExecuteNonQuery(string ddl) {
using (var conn = new SqlConnection(connectionString))
using (var cmd = new SqlCommand(ddl, conn)) {
conn.Open();
cmd.ExecuteNonQuery();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment