-
Notifications
You must be signed in to change notification settings - Fork 3
/
HomeControllerTests.cs
97 lines (83 loc) · 3.57 KB
/
HomeControllerTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System.Linq;
using System.Web.Mvc;
using Autofac;
using ControllerTests.MigrateDb;
using ControllerTests.Web;
using ControllerTests.Web.Controllers;
using ControllerTests.Web.Helpers;
using ControllerTests.Web.Models;
using NHibernate;
using NHibernate.Linq;
using NSubstitute;
using Shouldly;
using Xunit;
namespace ControllerTests.Tests
{
public class HomeControllerTests : MvcControllerTestBase<HomeController, ISession>
{
internal static TestSetup<ISession> MssqlTestSetup = new TestSetup<ISession>(
ContainerConfig.BuildContainer(),
builder =>
{
builder.RegisterType<LocalDb>().AsSelf(); // this needs to be registered so it's Disposed properly
// changing the ISession to a singleton so that the two ISession Resolve() calls
// produce the same instance such that the transaction includes all test activity.
builder.Register(context =>
{
var connString = context.Resolve<LocalDb>().OpenConnection().ConnectionString;
// migrate empty db
Program.Main(new[] { connString });
return NhibernateConfig.CreateSessionFactory(connString).OpenSession();
})
.As<ISession>()
.SingleInstance();
},
session => session.BeginTransaction(),
session => session.Transaction.Dispose(), // tear down transaction to release locks
session =>
{
NhibernateConfig.CompleteRequest(session);
session.Clear(); // this is to ensure we don't get ghost results from the NHibernate cache
});
public HomeControllerTests() : base(MssqlTestSetup) { }
[Fact]
public void Given3ICs_WhenGetIndex_ThenAll3ICsAreReturned()
{
new[]
{
new IntegratedCircuit {Code = "1", Description = "Test1"},
new IntegratedCircuit {Code = "2", Description = "Test2"},
new IntegratedCircuit {Code = "3", Description = "Test3"},
}.ForEach(ic => Session.Save(ic));
Session.Flush();
var model = ((HomeController.IndexVM)((ViewResult)ActAction(c => c.Index())).Model).ICs;
model.Count().ShouldBe(3);
model.Last().Description.ShouldBe("Test3");
}
[Fact]
public void GivenNoICs_WhenPostCreateIC_ThenStoreContainsNewIC()
{
const string newIcCode = "556";
const string newIcDescription = "dual timer";
ActAction(c => c.CreateIC(newIcCode, newIcDescription));
var newIc = Session.Query<IntegratedCircuit>().Single();
newIc.Code.ShouldBe(newIcCode);
newIc.Description.ShouldBe(newIcDescription);
}
[Fact]
public void Given3ICs_WhenDeleteICsWithAll3Ids_ThenTheStoreIsEmpty()
{
SubstituteAndConfigure<IDevAccessChecker>().UserHasDevAccess().Returns(true);
var newICs = new[]
{
new IntegratedCircuit {Code = "1", Description = "Test1"},
new IntegratedCircuit {Code = "2", Description = "Test2"},
new IntegratedCircuit {Code = "3", Description = "Test3"},
};
newICs.ForEach(ic => Session.Save(ic));
Session.Flush();
ActAction(c => c.DeleteICs(newICs.Select(i => i.Id).ToArray()));
Session.Query<IntegratedCircuit>().Count().ShouldBe(0);
}
}
}