Last active
October 4, 2019 02:18
-
-
Save rippo/5684412 to your computer and use it in GitHub Desktop.
To show how you could test a NHibernate session.Query<T> on a controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[NHibernateActionFilter] | |
public class MemberController | |
private readonly IDbService db; | |
public MemberController(IDbService db) | |
{ | |
this.db = db; | |
} | |
public ActionResult CompanyPage(int id) | |
{ | |
var model = new CompanyPageViewModel | |
{ | |
CompanyPage = db.Query<BobMemberCompanyPage>().FirstOrDefault(w => w.Id == id) | |
}; | |
return View("CompanyPage", model); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DbService : IDbService | |
{ | |
[Dependency] | |
public ISessionFactory SessionFactory { get; set; } | |
[Dependency] | |
public ISession Session | |
{ | |
get { return SessionFactory.GetCurrentSession(); } | |
} | |
public IQueryable<T> Query<T>() where T : class | |
{ | |
return Session.Query<T>(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FakeDbService : IDbService | |
{ | |
public FakeDbService() | |
{ | |
} | |
public ISessionFactory SessionFactory { get; set; } | |
public ISession Session | |
{ | |
get | |
{ | |
var mock = new Mock<ISession>(); | |
mock.Setup(w => w.Save(It.IsAny<object>())); | |
return mock.Object; | |
} | |
} | |
public IQueryable<T> Query<T>() where T : class | |
{ | |
return Sets[typeof(T)] as IQueryable<T>; | |
} | |
public void AddSet<T>(IQueryable<T> objects) | |
{ | |
Sets.Add(typeof(T), objects); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MvcApplication : HttpApplication | |
{ | |
public static ISessionFactory SessionFactory { get; set; } | |
.... | |
protected void Application_Start() | |
{ | |
SessionFactory = SetUpNhibernate(); | |
Bootstrapper.Initialise(); //makes call to setup unity... | |
.... | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IDbService | |
{ | |
ISessionFactory SessionFactory { get; set; } | |
ISession Session { get; } | |
IQueryable<T> Query<T>() where T : class; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestMethod] | |
public void test_company_view_for_an_unkown_user() | |
{ | |
var db = new FakeDbService(); | |
db.AddSet(TestData.BobMemberCompanyPages); | |
var controller = new MemberController(db); | |
var result = controller.CompanyPage(12) as ViewResult; | |
const string expected = "CompanyPage"; | |
Assert.IsNotNull(result, "Should have returned a ActionResult"); | |
Assert.AreEqual(expected, result.ViewName); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestData | |
{ | |
public static IQueryable<BobMemberCompanyPage> BobMemberCompanyPages | |
{ | |
get | |
{ | |
return | |
new List<BobMemberCompanyPage> { | |
new BobMemberCompanyPage { Id = 12, CompanyName = "company", Show = true} | |
}.AsQueryable(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
container.RegisterType<IDbService, DbService>(); | |
container.RegisterType<ISession>(); | |
//Session Factory | |
container.RegisterInstance<ISessionFactory>(MvcApplication.SessionFactory); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment