This is a migrated thread and some comments may be shown as answers.

Using JustMock with a portion of my NHibernate Repository

1 Answer 91 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Jack E.
Top achievements
Rank 1
Jack E. asked on 11 May 2010, 12:53 AM
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:
        [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<stringobject>>())).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<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?

Thanks,
Jack

1 Answer, 1 is accepted

Sort by
0
Accepted
Ricky
Telerik team
answered on 11 May 2010, 08:29 AM
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.

Regards,
Mehfuz


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.
Tags
General Discussions
Asked by
Jack E.
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Share this question
or