Visual Studio 2010 dường như đang trộn các thư viện được đề cập ở trên.Tại sao Visual Studio 2010 lại trộn System.Web và System.Web.Abstractions?
Mẫu mã này lấy từ sách "Pro ASP.NET MVC2 Framework" của Steven Sanderson.
[TestMethod]
public void HomePage_Recognizes_New_Visitor_And_Sets_Cookie()
{
// Arrange: First prepare some mock context objects
var mockContext = new Mock<HttpContextBase>();
var mockRequest = new Mock<HttpRequestBase>();
var mockResponse = new Mock<HttpResponseBase>();
// The following lines define associations between the different mock objects
// (i.e. tells Moq what alue to use for tMockContext.Request)
mockContext.Setup(x=> x.Request).Returns(mockRequest.Object);
mockContext.Setup(x=> x.Response).Returns(mockResponse.Object);
mockRequest.Setup(x=> x.Cookies).Returns(new HttpCookieCollection());
mockResponse.Setup(x=> x.Cookies).Returns(new HttpCookieCollection());
var homeController = new HomeController();
var requestContext = new RequestContext(mockContext.Object, new RouteData());
homeController.ControllerContext = new ControllerContext(requestContext, homeController);
// Act
ViewResult viewResult = homeController.HomePage();
// Assert
Assert.AreEqual(String.Empty, viewResult.ViewName);
Assert.IsTrue((bool)viewResult.ViewData["IsFirstVisit"]);
Assert.AreEqual(1, homeController.Response.Cookies.Count);
Assert.AreEqual(bool.TrueString, homeController.Response.Cookies["HasVisitedBefore"].Value);
}
Dự án của tôi tham chiếu thư viện System.Web và System.Web.Abstractions.
Khi tập tin mã duy nhất là "sử dụng System.Web" Tôi nhận được hai lỗi:
- (dòng 25 dưới chữ 'Khẳng định') Các loại 'System.Web.HttpResponseBase' được định nghĩa trong một lắp ráp không được tham chiếu. Bạn phải thêm tham chiếu đến assembly 'System.Web.Abstractions, Version = 3.5.0.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35'.
- (Dòng 25 và 26 dưới từ 'Cookie') 'System.Web.HttpResponseBase' không chứa định nghĩa cho 'Cookie' và không có phương pháp mở rộng 'Cookies' chấp nhận đối số đầu tiên của loại 'System.Web.HttpResponseBase 'có thể được tìm thấy (là bạn thiếu một chỉ thị sử dụng hoặc tham khảo một hội?)
Nếu tôi thêm 'sử dụng System.Web.Abstractions' vào tập tin mã và xây dựng dự án, các lỗi trên đi nhưng sau đó sau đó tôi nhận được lỗi sau:
- Tên loại hoặc không gian tên 'Tóm tắt' không tồn tại trong không gian tên 'System.Web' (bạn đang thiếu một hội đồng tham khảo?)
Điều thú vị là, trong cả hai trường hợp khi tôi đặt dấu chấm sau khi Trả lời, Intellisense nhắc tôi với các lựa chọn đúng (tức là Response.Cookies). Có vẻ như Intellisense có thông tin về HttpResponseBase rằng công cụ xây dựng không có.
Bất kỳ ý tưởng nào có thể gây ra điều này?
Đó là một tham chiếu bị nhỡ. Tôi đã xem xét tham chiếu System.Web.Abstractions trong dự án WebUI của tôi chứ không phải dự án UnitTests. Cảm ơn. Các hành vi vẫn còn loại lẻ. – Jason
Ngọt ngào. Cảm ơn ! –