views:

36

answers:

1

Visual Studio 2010 seems to be mixing up the above mentioned libraries.

This code sample is from the book "Pro ASP.NET MVC2 Framework" by Steven Sanderson.

01 [TestMethod]
02 public void HomePage_Recognizes_New_Visitor_And_Sets_Cookie() {
03  // Arrange: First prepare some mock context objects
04  var mockContext = new Mock<HttpContextBase>();
05  var mockRequest = new Mock<HttpRequestBase>();
06  var mockResponse = new Mock<HttpResponseBase>();
07
08  // The following lines define associations between the different mock objects
09  // (i.e. tells Moq what alue to use for tMockContext.Request)
10  mockContext.Setup(x=> x.Request).Returns(mockRequest.Object);
11  mockContext.Setup(x=> x.Response).Returns(mockResponse.Object);
12  mockRequest.Setup(x=> x.Cookies).Returns(new HttpCookieCollection());
13  mockResponse.Setup(x=> x.Cookies).Returns(new HttpCookieCollection());
14
15  var homeController = new HomeController();
16  var requestContext = new RequestContext(mockContext.Object, new RouteData());
17  homeController.ControllerContext = new ControllerContext(requestContext, homeController);
18
19  // Act
20  ViewResult viewResult = homeController.HomePage();
21
22  // Assert
23  Assert.AreEqual(String.Empty, viewResult.ViewName);
24  Assert.IsTrue((bool)viewResult.ViewData["IsFirstVisit"]);
25  Assert.AreEqual(1, homeController.Response.Cookies.Count);
26  Assert.AreEqual(bool.TrueString, homeController.Response.Cookies["HasVisitedBefore"].Value);
27 }

My project references the System.Web and System.Web.Abstractions libraries.

When the code file is only "using System.Web" I get two errors:

  1. (Line 25 under the word 'Assert') The type 'System.Web.HttpResponseBase' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Web.Abstractions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
  2. (Lines 25 and 26 under the word 'Cookies') 'System.Web.HttpResponseBase' does not contain a definition for 'Cookies' and no extension method 'Cookies' accepting a first argument of type 'System.Web.HttpResponseBase' could be found (are you missing a using directive or an assembly reference?)

If I add "using System.Web.Abstractions" to the code file and build the project, the above errors go away but then then I get the following error:

  1. The type or namespace name 'Abstractions' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)

Interestingly, in both cases when I place a dot after Response, Intellisense prompts me with the correct choices (i.e. Response.Cookies). It seems like Intellisense has information about HttpResponseBase that the build engine does not.

Any idea what may be causing this?

A: 

Can you double check that under your Project References you have both System.Web and System.Web.Abstractions?

Using the source code for the book downloaded from the Apress site, I can build your code without errors, but when I remove the reference to System.Web.Abstractions, I get the same behavior, including the intellisense information prompts you noted.

adrift
It was a missed reference. I was looking at the System.Web.Abstractions reference in my WebUI project rather than the UnitTests project. Thanks. The behavior is still kind of odd.
Jason