chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
Hi,

I'm getting tests failing all over the place as a result of upgrading TypeMock to 4.2.3 from 4.1.0. I haven't touched any of the tests other than to upgrade the version of typemock. I don't really know where to begin because there's so many tests failing for so many different reasons. Have you made some really fundamental changes to the way everything works between the two versions? Here's one test that's failing:
      [Test]
      public void Render() {
         Mock extensionMock = MockManager.Mock<AdminUserFormTestExtension>();
         Mock placeHolderMock = MockManager.Mock<PlaceHolder>();
         StringWriter stringWriter = new StringWriter();
         HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);

         extensionMock.ExpectCall("AddAttributesToRender").Args(htmlWriter);
         placeHolderMock.ExpectCall("RenderControl").Args(htmlWriter);

         AdminUserFormTestExtension extension = new AdminUserFormTestExtension();
         extension.Render(htmlWriter);
      }


Which is testing this method:
      protected override void Render(HtmlTextWriter writer) {
         AddAttributesToRender(writer);
         _placeHolder.RenderControl(writer);
      }


If you need any further info, just let me know! The PlaceHolder that I'm mocking is null when I run the test

Thanks,
Simon
asked by yourgolftravel (720 points)

10 Answers

0 votes
Hi,

One of the breaking changes we have made is the default handling of constructors on mocks created using the generic API.

In previous versions, by default, the ctors on mocks created using generic API's were not mocked, but were mocked when using the none generic API.

In the new version the ctor is mocked by default for all API's.

In order to fix your test you can use the following code:
MockManager.Mock<AdminUserFormTestExtension>(Constructor.NotMocked); 
Mock placeHolderMock = MockManager.Mock<PlaceHolder>(Constructor.NotMocked); 
answered by lior (13.2k points)
0 votes
Lior beat me to it. Adding "Constructor.NotMocked" fixes it.

In this particular case, you could add it just to the mock for the AdminUserFormTestExtension and the test will pass because you're setting up the expectations for all the calls to the PlaceHolder. I set up the following and the test passes. The only difference is the Constructor.NotMocked for AdminUserFormTestExtension:

// Class to test
public class AdminUserFormTestExtension
{
  private PlaceHolder _placeHolder = new PlaceHolder();
  
  public void Render(HtmlTextWriter writer)
  {
    AddAttributesToRender(writer); 
    _placeHolder.RenderControl(writer); 
  }
  
  protected void AddAttributesToRender(HtmlTextWriter writer)
  {
    //Doesn't matter because we're mocking this call.
  }
}

[TestFixture]
[VerifyMocks]
public class MyTestFixture
{
  [Test] 
  public void Render()
  { 
    // This is the only changed line:
    Mock extensionMock = MockManager.Mock<AdminUserFormTestExtension>(Constructor.NotMocked); 
    // Everything else is the same...
    Mock placeHolderMock = MockManager.Mock<PlaceHolder>(); 
    StringWriter stringWriter = new StringWriter(); 
    HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter); 
    
    extensionMock.ExpectCall("AddAttributesToRender").Args(htmlWriter); 
    placeHolderMock.ExpectCall("RenderControl").Args(htmlWriter); 
    
    AdminUserFormTestExtension extension = new AdminUserFormTestExtension(); 
    extension.Render(htmlWriter); 
  }
}
answered by tillig (6.7k points)
0 votes
Aha. Gotcha. Fixed most of the failing tests now - thanks for pointing that out to me. I'm struggling still with some tests in which I'm creating Users and adding them to my membership store. Here's the test code:
      private static MembershipUserCollection CreateDefaultUsers() {
         MembershipUserCollection users = new MembershipUserCollection();
            users.Add(new MembershipUser("AspNetSqlMembershipProvider", "simon.rigby", new Guid("05b75472-2a41-4cef-84ce-cea5517a80b9"), "simon@yourgolftravel.com",
                                      "question", null, true, false, new DateTime(2007, 10, 31), new DateTime(2007, 10, 31), new DateTime(2007, 10, 31),
                                      new DateTime(2007, 10, 31), new DateTime(2007, 10, 31)));
         return users;
      }



      [Test]
      public void AddUser() {
         MembershipUserCollection defaultUsers = CreateDefaultUsers();
         MembershipUserCollection users = new MembershipUserCollection();
         MembershipUser user = new MembershipUser("AspNetSqlMembershipProvider", "bob.smith", new Guid("abd70434-bb5d-4905-86a2-19d9ac769086"), "bob@yourgolftravel.com",
                                                  "question", null, true, false, new DateTime(2007, 10, 31), new DateTime(2007, 10, 31), new DateTime(2007, 10, 31),
                                                  new DateTime(2007, 10, 31), new DateTime(2007, 10, 31));
         users.Add(user);
         users.Add(defaultUsers["simon.rigby"]);
         string defaultPassword = "password";
         TextBox tbUserName = new TextBox();
         TextBox tbEmail = new TextBox();
         CheckBoxList checkBoxList = CreateRoleCheckBoxList();
         checkBoxList.Items[1].Selected = true;
         checkBoxList.Items[2].Selected = true;
         string[] roles = new string[] {"SalesAdministrator", "Salesperson"};
         Label lblAddUser = new Label();
         tbUserName.Text = "bob.smith";
         tbEmail.Text = "bob@yourgolftravel.com";
         Table tblUsers = CreateUsersTable(false);

         Mock extensionMock = MockManager.Mock<AdminUserFormTestExtension>();
         Mock membershipMock = MockManager.Mock(typeof (Membership));
         Mock webConfigurationManagerMock = MockManager.Mock(typeof (WebConfigurationManager));
         MockObject appSettingsMockObject = MockManager.MockObject<NameValueCollection>();
         Mock rolesMock = MockManager.MockAll(typeof (Roles));
         rolesMock.AlwaysReturn("GetAllRoles", _roles.ToArray());
         extensionMock.ExpectAndReturn("FindControl", tbUserName).Args("tbUserName_AdminUserForm");
         extensionMock.ExpectAndReturn("FindControl", tbEmail).Args("tbEmail_AdminUserForm");
         extensionMock.ExpectAndReturn("FindControl", checkBoxList).Args("cblUserRoles0_AdminUserForm");
         extensionMock.ExpectAndReturn("FindControl", lblAddUser).Args("lblAddUser_AdminUserForm");
         extensionMock.ExpectAndReturn("FindControl", tblUsers).Args("tblUsers_AdminUserForm");
         webConfigurationManagerMock.ExpectGet("AppSettings", appSettingsMockObject.Object);
         appSettingsMockObject.ExpectAndReturn("Get", defaultPassword).Args("defaultPassword");
         membershipMock.ExpectAndReturn("CreateUser", user).Args("bob.smith", defaultPassword, "bob@yourgolftravel.com");
         rolesMock.ExpectCall("AddUserToRoles").Args("bob.smith", roles);
         rolesMock.ExpectAndReturn("IsUserInRole", false).Args("bob.smith", _roles[0]);
         rolesMock.ExpectAndReturn("IsUserInRole", true).Args("bob.smith", _roles[1]);
         rolesMock.ExpectAndReturn("IsUserInRole", true).Args("bob.smith", _roles[2]);
         membershipMock.ExpectAndReturn("GetAllUsers", users);

         AdminUserFormTestExtension extension = new AdminUserFormTestExtension();
         extension.AddUser(new Button(), EventArgs.Empty);
         Assert.AreEqual("ok", lblAddUser.CssClass);
         Assert.AreEqual("User bob.smith was created ok", lblAddUser.Text);
         Assert.AreEqual(6, tblUsers.Rows.Count);
         Assert.AreEqual("", tblUsers.Rows[1].CssClass);
         Assert.AreEqual("alternate", tblUsers.Rows[2].CssClass);
         Assert.AreEqual("", tblUsers.Rows[3].CssClass);
         Assert.AreEqual("alternate", tblUsers.Rows[4].CssClass);
         Assert.AreEqual("", tblUsers.Rows[5].CssClass);
         Assert.AreEqual("bob.smith", tblUsers.Rows[2].Cells[1].Text);
      }



The error starts in the MembershipUser constructor within CreateDefaultUsers, but I'm a bit stuck after that. Any ideas? I've tried mocking the MembershipUser, but that just stops the actual MembershipU
answered by yourgolftravel (720 points)
0 votes
This is a pretty complex case and there are a lot of things going on in that AddUser test that you didn't post code for. That is, I see a lot of expectations set up for "FindControl" calls but there's no code here that calls "FindControl." Can you simplify the reproduction a bit to just the parts that are failing? It'll be easier to troubleshoot and you may actually discover what's wrong as you simplify the case.

For example, if you remove everything from "string defaultPassword =..." down in the test, does it still fail? If so, that's your simplified reproduction. It'll be easier to start investigation there.
answered by tillig (6.7k points)
0 votes
You're right of course. Apologies for that. Here's a much simpler case that still fails. It's the creation of the new MembershipUser that is failing:
      [Test]
      public void AddUserCheck() {
         MembershipUserCollection users = new MembershipUserCollection();
         MembershipUser user = new MembershipUser("AspNetSqlMembershipProvider", "bob.smith", new Guid("abd70434-bb5d-4905-86a2-19d9ac769086"), "bob@yourgolftravel.com",
                                        "question", null, true, false, new DateTime(2007, 10, 31), new DateTime(2007, 10, 31), new DateTime(2007, 10, 31),
                                        new DateTime(2007, 10, 31), new DateTime(2007, 10, 31));
         users.Add(user);
      }


Thanks,
Simon
answered by yourgolftravel (720 points)
0 votes
I just ran that test in a standalone project using Snippet Compiler and it passed without issue. I noticed there's no mocking going on in there, which, in my experience, means one of two things:

1) You have something in your test setup that is mocking something that shouldn't be mocked, at least for this test.
2) Somewhere else in the test fixture you've got a MockAll set up and it's causing you trouble. There's a little more about MockAll clearing here: https://www.typemock.com/community/viewt ... p?=&p=1401

Usually when I see weirdness like this in our tests, it's a MockAll thing.

I noticed that the exception was something about configuration - something else you could check is to see if either you're mocking configuration (maybe some more code needs to be set up there) or if your unit test assembly has an app.config file that has some configuration specified that may be incorrect.

All guesses on my part, but that's where I'd start looking.
answered by tillig (6.7k points)
0 votes
You're a genius. I am indeed doing some mocking in my SetUp method, which was causing problems here. Commenting that method out caused this test to pass, so I'll have to be more careful about where I call the SetUp mocking method in future! Thanks for all your help with this
answered by yourgolftravel (720 points)
0 votes
Hi

I am using typemock V4.1.0...but while debugging the examples which are given while installing the product is failing ..but they are ok with 4.2.3 version...so i want to know do you have done some major changes in the newer version

Thank u
answered by sunny0102 (680 points)
0 votes
Hi,

Yes we have done several major changes in the new version.

Specifically what I think is confusing you is the enhancement we have made to the integration with the IDE (better debugging abilities)

In the 4.1 version putting a break point inside the recording block
confused the Isolator, this was resolve in the 4.2 version.
answered by lior (13.2k points)
0 votes
Hi

Thank u lior for your quick replay........... :D
answered by sunny0102 (680 points)
...