示例下载:
http://files.cnblogs.com/chsword/MyTestMvc.rar
顺遍说一下建立测试的方法
本身Asp.netMvc是提供有测试功能的
在新建Asp.netMVCApplication时,点击确定,会跳出一个Create Project Test的询问
如果选Yes就自动建立一个 "工程名+Test"的测试工程
测试工程要引用Rhino.Mock(最新版本3.4)这一开源项目(下载地址 http://www.ayende.com/projects/rhino-mocks/downloads.aspx)
测试代码没有什么好说的了,看了示例自然了解,Scott的 MockHelpers使测试更加方便
可以利用类似以下代码进行测试
[TestMethod]

public
void
Index()
{
HomeController home = new HomeController();
var viewengin = new FakeViewEngine();
home.ViewEngine = viewengin;
MockRepository mock = new MockRepository();

using (mock.Record()) {
mock.SetFakeControllerContext(home);
}

using (mock.Playback()) {
home.Index();
Assert.AreEqual("Index", viewengin.ViewContext.ViewName);
}
}
示例说明
示例中有个登录功能
有两个页面
/Post/Index页面只有登录后才能查看
/Post/Post页面只有登录后的admin用户才能查看
否则就会Error
在验证登录时我写了一个扩展方法
public
static
class
ControllerExtension

{

public static bool IsPost(this Controller controller) {
return controller.Request.Form.Count > 0;
}
}
登录时我使用Session来保存用户登录信息
所有用户信息我在 Global中进行初始化
RegisterRoutes(RouteTable.Routes);
//
将Application代替数据库用
Application[
"
Posts
"
]
=
new
List
<
Post
>
();
//
一个账号集合
Application[
"
Accounts
"
]
=
new
List
<
Account
>
();
//
一个帖子集合
//
初始化两个账号
List
<
Account
>
la
=
Application[
"
Accounts
"
]
as
List
<
Account
>
;
la.Add(
new
Account()
//
这个是管理员

{
Username = "admin",
Password = "admin"
}
);
la.Add(
new
Account()
//
这个是普通用户

{
Username = "user",
Password = "user"
}
);
具体的内容看代码就行了,感冒了...很是难受...手纸用了三卷了....( 乱世重典)
Asp.net Mvc Framework 系列
http://files.cnblogs.com/chsword/MyTestMvc.rar
顺遍说一下建立测试的方法
本身Asp.netMvc是提供有测试功能的
在新建Asp.netMVCApplication时,点击确定,会跳出一个Create Project Test的询问
如果选Yes就自动建立一个 "工程名+Test"的测试工程
测试工程要引用Rhino.Mock(最新版本3.4)这一开源项目(下载地址 http://www.ayende.com/projects/rhino-mocks/downloads.aspx)
测试代码没有什么好说的了,看了示例自然了解,Scott的 MockHelpers使测试更加方便
可以利用类似以下代码进行测试

















示例中有个登录功能
有两个页面
/Post/Index页面只有登录后才能查看
/Post/Post页面只有登录后的admin用户才能查看
否则就会Error
在验证登录时我写了一个扩展方法








登录时我使用Session来保存用户登录信息
所有用户信息我在 Global中进行初始化


















具体的内容看代码就行了,感冒了...很是难受...手纸用了三卷了....( 乱世重典)
Asp.net Mvc Framework 系列