DavidS
DavidS

Reputation: 2283

How to write the MSpec test for code containing calls to HttpContext using fakeiteasy?

I am getting the proverbial knickers in a twist. For this very simple code:

public ActionResult Add()
    {

        this.HttpContext.Items["pm-page-title"] = "Some title";

        return this.View();
    }

How do I go about writing the MSpec test, using fakeiteasy, to verify that a view is returned and more pertinently that the page title is set correctly?

TIA,

David

Upvotes: 0

Views: 552

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

// arrange
var sut = new SomeController();
sut.ControllerContext = A.Fake<ControllerContext>();
var fakeContext = A.Fake<HttpContextBase>();
A.CallTo(() => sut.ControllerContext.HttpContext).Returns(fakeContext);
A.CallTo(() => fakeContext.Items).Returns(new Hashtable());

// act
var actual = sut.Add();

// assert
Assert.AreEqual("Some title", (string)fakeContext.Items["pm-page-title"]);

Upvotes: 1

Related Questions