Created
December 21, 2010 04:53
-
-
Save skoon/749512 to your computer and use it in GitHub Desktop.
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
using Xunit; | |
using VirtualRoundtableData; | |
using System; | |
using Models; | |
namespace UnitTests | |
{ | |
public class when_using_the_user_repository: TestBase | |
{ | |
UserRepository _repository; | |
public when_using_the_user_repository() | |
{ | |
_repository = new UserRepository(GetMockSessionFactoryCreatorFor<User>()); | |
} | |
[Fact] | |
public void should_get_user_by_id() | |
{ | |
Assert.NotNull(_repository.getById(0)); | |
} | |
[Fact] | |
public void should_return_all_users() | |
{ | |
Assert.NotEmpty(_repository.getAll()); | |
} | |
[Fact] | |
public void should_get_a_list_of_users_when_passed_a_search_phrase() | |
{ | |
Assert.NotEmpty(_repository.Search("foo")); | |
} | |
} | |
} | |
//TestBase.cs | |
using System; | |
using NHibernate; | |
using Moq; | |
using VirtualRoundtableData; | |
using System.Collections.Generic; | |
namespace UnitTests | |
{ | |
public abstract class TestBase | |
{ | |
public bool DataSaved { get; set; } | |
protected ISessionFactoryCreator GetMockSessionFactoryCreatorFor<T>() | |
{ | |
var mockSessionFactoryCreator = new Mock<ISessionFactoryCreator>(); | |
mockSessionFactoryCreator.Setup(fc => fc.CreateSession()).Returns(GetMockSessionFactoryFor<T>().Object); | |
return mockSessionFactoryCreator.Object; | |
} | |
protected Mock<ISessionFactory> GetMockSessionFactoryFor<T>() | |
{ | |
var mockSessionFactory = new Mock<ISessionFactory>(); | |
mockSessionFactory.Setup(repo => repo.OpenSession()).Returns(GetMockSessionFor<T>().Object); | |
return mockSessionFactory; | |
} | |
protected Mock<ISession> GetMockSessionFor<T>() | |
{ | |
var mockSession = new Mock<ISession>(); | |
mockSession.Setup(session => session.Get<T>(It.IsAny<int>())).Returns(System.Activator.CreateInstance<T>()); | |
mockSession.Setup(session => session.Save(It.IsAny<T>())).Callback( () => { DataSaved = true;}); | |
mockSession.Setup(session => session.Delete(It.IsAny<T>())).Callback(() => { DataSaved = true; }); | |
mockSession.Setup(session => session.CreateCriteria(typeof(T))).Returns( createMockCriteria<T>()); | |
mockSession.Setup(session => session.Update(It.IsAny<T>())).Callback(() => {DataSaved = true;}); | |
mockSession.Setup(session => session.CreateQuery(It.IsAny<string>())).Returns(createMockQuery<T>()); | |
mockSession.Setup(session => session.BeginTransaction()).Returns(createMockTransaction<T>()); | |
return mockSession; | |
} | |
private ICriteria createMockCriteria<T>() | |
{ | |
var mockCriteria = new Mock<ICriteria>(); | |
mockCriteria.Setup(criteria => criteria.List<T>()).Returns(createMockList<T>()); | |
return mockCriteria.Object; | |
} | |
public ITransaction createMockTransaction<T>() | |
{ | |
var mockTransaction = new Mock<ITransaction>(); | |
mockTransaction.Setup(trans => trans.Commit()).Callback(() => { DataSaved = true; }); | |
return mockTransaction.Object; | |
} | |
public IQuery createMockQuery<T>() | |
{ | |
var mockQuery = new Mock<IQuery>(); | |
mockQuery.Setup(query => query.List<T>()).Returns(createMockList<T>()); | |
return mockQuery.Object; | |
} | |
private List<T> createMockList<T>() | |
{ | |
return new List<T> { Activator.CreateInstance<T>() }; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment