Using JustMock with a portion of my NHibernate Repository

2 posts, 1 answers
  1. Jack E.
    Jack E. avatar
    2 posts
    Member since:
    Sep 2007

    Posted 10 May 2010 Link to this post

    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:
            public void SetUp() 
                _provider = new NHibernateMembershipProvider(CreateMockUserRepository()); 
                var cs = ConfigurationManager.ConnectionStrings; 
                var config = new NameValueCollection 
                _provider.Initialize(config["name"], config); 
            // Doesn't work 
            public void CanGetUserByEmail() 
                string email = ""
                string username = _provider.GetUserNameByEmail(email); // always null so it always fails. Repo mocked below 
                Assert.AreEqual("marykate2", username); 
            // Works fine 
            public void CanGetUserByUserKey() 
                string email = ""
                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.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 = "" 
                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
                   user = _userRepository.FindOne(new Dictionary<stringobject> {{"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<stringobject> {{"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?

  2. Answer
    Ricky avatar
    467 posts

    Posted 11 May 2010 Link to this post

    Hi Jack E.,

    Yes you are right, There is an issue with Mock.Arrange parsing with dictionary that's why the following setup is not working correctly.

    Mock.Arrange(() => mockedRepository.FindOne(Arg.Any<IDictionary<string, object>>())). 

    This is already fixed and will be available in the next build , next week.


    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. DevCraft R3 2016 release webinar banner
Back to Top