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:
- (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'.
- (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:
- 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?