Hi,
I'm trying to mock out some functions in my repository. Some work, some don't. This is a combination of NUnit, NHibernate, and indirectly, ASP.NET MVC. I'm in the process of writing tests for a custom ASP.NET Membership provider I'm writing.
The test fixture setup, two tests and the various supporting code:
And the method I'm trying to test:
So for whatever reason _userRepository.FindOne(new Dictionary<string, object> {{"Email", email}}), the results are ALWAYS null. It seem the mock framework isn't intercepting this call, assuming I'm doing things correctly. Is there a better way to do this?
Thanks,
Jack
I'm trying to mock out some functions in my repository. Some work, some don't. This is a combination of NUnit, NHibernate, and indirectly, ASP.NET MVC. I'm in the process of writing tests for a custom ASP.NET Membership provider I'm writing.
The test fixture setup, two tests and the various supporting code:
| [SetUp] |
| public void SetUp() |
| { |
| ServiceLocatorInitializer.Init(); |
| _provider = new NHibernateMembershipProvider(CreateMockUserRepository()); |
| var cs = ConfigurationManager.ConnectionStrings; |
| var config = new NameValueCollection |
| { |
| {"applicationName", "PerformanceTracker"}, |
| {"name", "NHibernateMembershipProvider"}, |
| {"requiresQuestionAndAnswer", "false"}, |
| {"connectionStringName", "membershipProviderConnectionString"} |
| }; |
| _provider.Initialize(config["name"], config); |
| } |
| // Doesn't work |
| [Test] |
| public void CanGetUserByEmail() |
| { |
| string email = "marykate2@gmail.com"; |
| string username = _provider.GetUserNameByEmail(email); // always null so it always fails. Repo mocked below |
| Assert.AreEqual("marykate2", username); |
| } |
| // Works fine |
| [Test] |
| public void CanGetUserByUserKey() |
| { |
| string email = "marykate2@gmail.com"; |
| MembershipUser usr = _provider.GetUser(Guid.Empty, false); |
| Assert.AreEqual(usr.Email, email); |
| } |
| private IRepositoryWithTypedId<User, Guid> CreateMockUserRepository() |
| { |
| var called = false; |
| var mockedDbContext = Mock.Create<IDbContext>(); |
| Mock.Arrange(() => mockedDbContext.BeginTransaction()).DoInstead(() => called = true); |
| var mockedRepository = Mock.Create<IRepositoryWithTypedId<User, Guid>>(); |
| Mock.Arrange( |
| () => |
| mockedRepository.Get(Arg.Any<Guid>())).Returns(CreateUser()); |
| Mock.Arrange( |
| () => |
| mockedRepository.FindOne(Arg.Any<IDictionary<string, object>>())).Returns(CreateUser()); |
| Mock.Arrange(() => mockedRepository.DbContext).Returns(mockedDbContext); |
| return mockedRepository; |
| } |
| private User CreateUser() |
| { |
| User user = CreateTransientUser(); |
| EntityIdSetter.SetIdOf<Guid> (user, new Guid(0x9b84e442, 0x125c, 0x487b, 0x9f, 0x17, 0x18, 0x26, 0xd8, 0xe4, 0x40, 0x72)); |
| return user; |
| } |
| /// <summary> |
| /// Creates a valid, transient User; typical of something retrieved back from a form submission |
| /// </summary> |
| private User CreateTransientUser() |
| { |
| User user = new User() |
| { |
| UserName = "marykate2", |
| FirstName = "Mary", |
| LastName = "Kate", |
| LastActivityDate = new DateTime(), |
| Email = "marykate2@gmail.com" |
| }; |
| return user; |
| } |
And the method I'm trying to test:
| /// <summary> |
| /// Gets the user name associated with the specified e-mail address. |
| /// </summary> |
| /// <returns> |
| /// The user name associated with the specified e-mail address. If no match is found, return null. |
| /// </returns> |
| /// <param name="email">The e-mail address to search for. </param> |
| public override string GetUserNameByEmail(string email) |
| { |
| User user = null; |
| _userRepository.DbContext.BeginTransaction(); |
| try |
| { |
| user = _userRepository.FindOne(new Dictionary<string, object> {{"Email", email}}); |
| } |
| catch (Exception e) |
| { |
| throw new ProviderException(e.Message); |
| } |
| if (user == null) |
| return String.Empty; |
| return user.UserName; |
| } |
So for whatever reason _userRepository.FindOne(new Dictionary<string, object> {{"Email", email}}), the results are ALWAYS null. It seem the mock framework isn't intercepting this call, assuming I'm doing things correctly. Is there a better way to do this?
Thanks,
Jack