System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.

Mar 28, 2011 at 12:10 AM

I really like the idea of this utility, unfortunately initial attempt at using it failed miserably.  Here is the code...

My original mspec test...

 

        [Subject(typeof(UserControllerTestFixture)), Tags("Authentication")]
        public partial class When_attempting_to_login_with_invalid_credentials : UserControllerContext
        {
            //Arrange
            static readonly LogonViewModel m_logonViewModel = new LogonViewModel
            {
                EmailAddress = "test@test.com",
                Password = "foo",
                RememberMe = false
            };

            Establish context = () =>
                m_registeredUserFactory.Setup(x => x.SelectByEmailAddress(m_logonViewModel.EmailAddress))
                    .Returns(m_registeredUserModel.Object);


            //Act
            Because of = () => m_userController.Logon(m_logonViewModel);


            //Assert
            It Should_notify_the_user_their_credentials_are_invalid = () =>
                m_userController.ViewData.ModelState.ContainsKey(Resources.LoginErrorKey).ShouldBeTrue();
        }

 

Here is the partial class generated my MsMspec...

 

     [TestClass]
    public partial class When_attempting_to_login_with_invalid_credentials
    {
        static new Dictionary<string, Action> __results = new Dictionary<string, Action>();
        
        [ClassInitialize]
        public static void _Setup(TestContext context)
        {
            MSMSpec.TestExecutionHelper.Process<When_attempting_to_login_with_invalid_credentials>(__results);
        }
        
        [TestMethod]
        public void It_Should_notify_the_user_their_credentials_are_invalid()
        {
            __results["Should_notify_the_user_their_credentials_are_invalid"]();
        }
    }

 

Here is the test error...

Test method SedonaItSolutions.DetroitCommunitySchools.Tests.When_attempting_to_login_with_invalid_credentials.It_Should_notify_the_user_their_credentials_are_invalid threw exception:
System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.

 

[TestMethod]
public void It_Should_notify_the_user_their_credentials_are_invalid()
{
  __results["Should_notify_the_user_their_credentials_are_invalid"](); <-Failing here
}

 

 

Coordinator
Jun 17, 2011 at 3:38 PM

Ouch...I just noticed your response...for some reason, I didn't get an email. Anyway...could you possibly show the UserControllerContext class as well. I'll take a look at this tonight.

Coordinator
Jun 17, 2011 at 3:43 PM

Also, you seem to be initialising m_logonViewModel inline. Consider ALWAYS putting these in Establish. Due to the way MSpec runs [statics everywhere], you should always have all initialisation in Establishes, rather than initialised inline.
This is a practice in general and not just for MSMspec.