0

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

DavidS
  • 2,179
  • 4
  • 26
  • 44

1 Answers1

1
// 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"]);
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks Darin. That was what I was looking for. However, could you possibly explain the code a bit more? I'm not too familiar with HttpContext and how did you know about A.CallTo(() => fakeContext.Items).Returns(new Hashtable());. That was the missing code for me. – DavidS Apr 18 '11 at 13:02