Skip to content

Instantly share code, notes, and snippets.

@bltavares
Created March 9, 2012 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bltavares/2008502 to your computer and use it in GitHub Desktop.
Save bltavares/2008502 to your computer and use it in GitHub Desktop.
C# Trasactional Tests
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Test
{
[TestClass]
public class TestTransactionClass : TransactionalTest
{
[TestMethod]
public void TestThereIsNoUser()
{
var expected = db.Users.Any();
Assert.IsFalse(expected);
}
[TestMethod]
public void TestUserCreation()
{
var user = Fixtures.UserFixture.GenerateDefault<User>();
db.SaveChanges();
var expected = db.Users.Any(c => c.Name == user.Name);
Assert.IsTrue(expected);
}
[TestMethod]
public void TestTeamCreation()
{
var team = Fixtures.TeamFixture.GenerateDefault<Team>();
db.SaveChanges();
var expected = db.Teams.Any(c => c.Id == team.Id);
Assert.IsTrue(expected);
}
[TestMethod]
public void TestTeamPlayerCreation()
{
var name = "Bruno";
var player = Fixtures.TeamPlayerFixture.GenerateDefault<TeamPlayer>(new { Name = name });
db.SaveChanges();
var expected = db.TeamPlayers.Any(c => c.Name == name);
Assert.IsTrue(expected);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Transactions;
namespace Test
{
public class TransactionalTest
{
protected TransactionScope scope;
protected Entities db;
[TestInitialize]
public void SetUpNewTransaction()
{
scope = new TransactionScope(TransactionScopeOption.RequiresNew);
db = new Entities();
Fixtures.FixtureRepo.db = db;
}
[TestCleanup]
public void DisposeDatabase()
{
Fixtures.FixtureRepo.db = null;
db.AcceptAllChanges();
db.Dispose();
scope.Dispose();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment