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
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hi

I'm using Nunit v2.5.3.9345. I tend to push lots of setup code into base classes and there are cases when test state just disappears.

This seems fixable - all classes in chain must have RunOnWeb attribute applied. This step is however easy to miss and no warnings are generated to inform about potential side effects.

   //pass
   [TestFixture]
   public class WhenSettingStateInBaseClass : TestContext
   {
      public WhenSettingStateInBaseClass()
      {

      }

      [Test]
      public void StateShouldBePersisted()
      {
         Assert.AreEqual(1, State);
      }
   }

   //fail
   [TestFixture, RunOnWeb]
   public class WhenSettingStateInBaseClassInWebContext : TestContext
   {
      public WhenSettingStateInBaseClassInWebContext()
      {
         //constructor seems to fire twice
      }

      [Test]
      public void StateShouldBePersisted()
      {
         Assert.AreEqual(1, State); //this assertion fails
      }
   }

   //the only way to fix WhenSettingStateInBaseClassInWebContext is to add [RunOnWeb] here but then same needs to happen for WhenSettingStateInBaseClass
   public class TestContext
   {
      protected int State;

      //[SetUp] //fails as well
      [TestFixtureSetUp]
      public void SetUp()
      {
         State = 1;
      }
   }
asked by pma (2.3k points)

2 Answers

0 votes
Hi,

Unfortunately, this is the way to go at the moment. Perhaps it is worth a blog post, but here are the details. You can't avoid creating an instance of the test class, since it's created by NUnit. However, we need another instance, created in a different AppDomain, where our Asp.Net request is executed. In order to create this instance and run the test code in the proper context, we need to apply the RunOnWeb attribute.

Somehow the RunOnWeb attribute, which is derived from TypeMock.DecoratorAttribute, is not applied to the code at the base class level. This might change in the future, though.

Artem
answered by ulu (1.7k points)
0 votes
Fair enough, thank you for explanation. It makes sense now :)
answered by pma (2.3k points)
...