0

I am using NUnit and Fake it easy. I am planning to Setup which can be reusable across the project.

here is my setupCode:

IClient fake;
SampleController sController;
[SetUp]
public void Setup()
{
    sController = new SampleController();
    fake = A.Fake<IClient>();
    fakeContext =new  FakeContext(sController);

    sController = new SampleController(fake);
}

here is my FakeContextClass:

 public class FakeContext
 {
     HttpSessionStateBase fakeHttpSession;
     HttpContextBase fakeHttpContext;
     ControllerContext ControllerContext;

     public  FakeContext(Controller controller)
     {
         try
         {
             fakeHttpSession = A.Fake<HttpSessionStateBase>();
             fakeHttpContext = A.Fake<HttpContextBase>();

             fakeHttpSession.Contents["TestSession"] = new SaSession()
             {
                 DefaultUID = 1,
                 Name = "system",
                 LastName = "Test",
             };
             var fake = (SaSession)fakeHttpSession.Contents["TestSession"];
             A.CallTo(() => fakeHttpContext.Session).Returns(fakeHttpSession);
             A.CallTo(() => fakeHttpSession["TestSession"]).Returns(fake);
             ControllerContext = new ControllerContext(fakeHttpContext, new RouteData(), A.Fake<ControllerBase>());

             controller.ControllerContext = ControllerContext;
         }
    }
}

During Setup, when I pass controller which needs to be faked, I am able to get faked controller context of the passed controller. Also I want the HttpSessionStatebase also to be faked along with the context during TestSetup,so i don't need to fake the HttpSession for every call in the unit test methods.

 My test Method:   

[Test]
public async Task DetailsView()
{
    int UID = 0;
    string Name = "MedPlus";

    A.CallTo(() => fake.GetDataAsync<IEnumerable<Group>>(fakeHttpSession, "/ManagementSvc/v1/Groups?UID=" + UID ));

    ////Act
    var actionResult = await sController.DetailsView(UID,Name);
    var viewResult = actionResult as ViewResult;
    var grpModel = viewResult.Model as Model;

    //Assert
    Assert.IsNotNull(actionResult);
    Assert.AreEqual("GrpDetails", viewResult.ViewName);
    Assert.IsInstanceOf(typeof(Model), viewResult.Model);
}

If I fake HttpSessionStatebase in Test method,then I am able to get the fakeHttpSession.

Is there a way to fake HttpSessionState(fakeHttpSession) and hte controller context once so that the same instances are used in every test in my assembly?

Blair Conrad
  • 233,004
  • 25
  • 132
  • 111
AMDI
  • 895
  • 2
  • 17
  • 40
  • I'm sorry. I'm not really sure what information you're after. Is your code not working as you'd like? Or are you just wondering about alternative implementations? – Blair Conrad Aug 30 '16 at 13:18
  • I want to Setup both controller context and session in Setup so that i can reuse same context and session across all test methods in test class – AMDI Aug 30 '16 at 13:30
  • Sounds like you want to use the NUnit attribute [SetUpFixture](https://github.com/nunit/docs/wiki/SetUpFixture-Attribute) to run your setup code, and isn't specific to FakeItEasy. I've marked the question as a duplicate of another question that asks how to run NUnit setup code once for an assembly. – Blair Conrad Aug 30 '16 at 13:49
  • Possible duplicate of [One Time initialization for Nunit](http://stackoverflow.com/questions/3188380/one-time-initialization-for-nunit) – Blair Conrad Aug 30 '16 at 13:49
  • As an alternative, since you're looking for one-time initialization and access to a couple of objects, you could just implement a [Singleton](http://csharpindepth.com/Articles/General/Singleton.aspx) and not worry about NUnit's special attributes for one-time setup. – Blair Conrad Aug 30 '16 at 13:58
  • Sorry for the confusion.I am looking for [Setup] attribute for each Test Class.I want to create and fake HttpSessionState in FakeContext class and reuse the fake session across the Test class – AMDI Aug 31 '16 at 06:49
  • In that case, your question is s duplicate of http://stackoverflow.com/questions/23158500/is-there-a-way-to-have-a-setupfixture-that-runs-once-per-class-instead-of-once-p. You want a [OneTimeSetUp](https://github.com/nunit/docs/wiki/OneTimeSetUp-Attribute) inside your test class. (Called [TestFixtureSetUp](http://www.nunit.org/index.php?p=fixtureSetup&r=2.6.4) in NUnit 2.x.) – Blair Conrad Aug 31 '16 at 10:26

0 Answers0