(编辑**我能够获得代码来编译并执行通过的单元测试。除了代码修复之外,VS2010无限期运行单元测试存在问题。我不得不替换一个已更改的dll文件在中止安装 vs 2012 期间。我在页面底部发布了对控制器和单元测试的更改。感谢所有发布答案的人。)
这是我在网上问过的第一个关于编码的问题。大约一年来,我一直在使用免费教程学习 C# .NET 和其他相关内容。到目前为止,我已经能够自己研究和解决所有问题。我现在开始冒险进入未知领域,我似乎找不到答案。
我一直在编写一个名为“在 7 天内逐步学习 MVC 模型视图控制器”的教程。这是链接: http: //www.codeproject.com/Articles/259560/Learn-MVC-Model-view-controller-Step-by-Step-in-7
我已经研究了错误的建议链接:
Error 'Mvccustomer.Models.Customer' does not contain a definition for 'DisplayCustomer' and no extension method 'DisplayCustomer' accepting a first argument of type 'Mvccustomer.Models.Customer' could be found (are you missing a using directive or an assembly reference?)
我遇到的问题是我似乎找不到类似的情况,有人正在使用类似的文件引用创建单元测试。请注意,我对 MVC 和单元测试完全陌生。
本教程的一个问题是,作者在视频中使用了一组命名空间/文件名,而在书面教程中使用了另一组。我能够自己解决这个问题。例如,一开始,他使用“Mvccustomer”作为项目名称,但在第一天的第 4 或第 5 个实验室时,他将其称为“Mvcinputscreen”。我怀疑问题在于项目中如何引用客户类,但到目前为止我无法弄清楚。
这是给我一个错误的单元测试:
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Mvccustomer.Models;
namespace MvcUnitTest
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void DisplayCustomer()
{
Customer obj = new Customer();
var varresult = obj.DisplayCustomer();
Assert.AreEqual("DisplayCustomer", varresult.ViewName);
}
}
}
这是客户类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Mvccustomer.Models;
namespace Mvccustomer.Models
{
public class Customer
{
public int Id { set; get; }
public string CustomerCode { set; get; }
public double Amount { set; get; }
}
}
这是显示客户视图:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Mvccustomer.Models.Customer>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>DisplayCustomer</title>
</head>
<body>
<div>
The customer id is <%= Model.Id %> <br />
The customer id is <%= Model.CustomerCode %> <br />
<%if (Model.Amount > 100)
{%>
This is a priveleged customer.
<% }
else
{ %>
This is a normal customer
<%} %>
</div>
</body>
</html>
和客户控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Mvccustomer.Models;
namespace Mvccustomer.Controllers
{
public class CustomerController : Controller
{
//
// GET: /Customer/
public ActionResult Index()
{
return View();
}
public ActionResult FillCustomer()
{
return View();
}
public ActionResult DisplayCustomer(Customer obj)
{
return View(obj);
}
}
}
如果我需要发布更多项目元素,请告诉我。当我构建 Mvccustomer 项目时,它编译得很好,没有错误。只有单元测试给我带来了麻烦。我想这个问题有点复杂,我热切地等待所有建设性批评带来的学习体验。谢谢你。
最终工作的编辑控制器和单元测试:
客户控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Mvccustomer.Models;
using Mvccustomer.Controllers;
namespace Mvccustomer.Controllers
{
public class CustomerController : Controller
{
//
// GET: /Customer/
public ActionResult Index()
{
return View();
}
public ActionResult FillCustomer()
{
return View();
}
[HttpPost]
public ActionResult DisplayCustomerView(CustomerModel customerModel)
{
var myView = View("DisplayCustomerView", customerModel);
//myView.ViewName = "DisplayCustomer";
return myView;
}
}
}
编辑单元测试:
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Mvccustomer.Models;
using Mvccustomer.Controllers;
using System.Web.Mvc;
namespace MvcUnitTest
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void DisplayCustomer()
{
// instantiates new instance of CustomerController class
CustomerController controller = new CustomerController();
var customer = new CustomerModel();
var customerViewActionResult = controller.DisplayCustomerView(customer);
var customerViewViewResult = customerViewActionResult as ViewResult;
Assert.AreEqual("DisplayCustomerView", customerViewViewResult.ViewName);
}
}
}