【发布时间】:2009-09-23 18:30:51
【问题描述】:
我正在使用 SubSonic 3,并且正在使用 ActiveRecord 方法。我的控制器中有一个简单的查询:
var posts = from p in Post.GetPaged(page ?? 0, 20)
orderby p.Published descending
select p;
“page”变量被定义为一个可为空的 int (int?)。
这是问题所在,当我运行以下测试时,它工作正常并通过:
[Test]
public void IndexWithNullPageShouldLoadFirstPageOfRecentPosts()
{
//act
var testPosts = new List<Post>()
{
new Post() {PostID = 1, BlogID = 1, Published = null, Body = "1"},
new Post() {PostID = 2, BlogID = 1, Published = DateTime.Now, Body = "2"},
new Post() {PostID = 3, BlogID = 1, Published = DateTime.Now.AddDays(1), Body = "3"}
};
Post.Setup(testPosts);
//act
var result = controller.Index(null) as ViewResult;
//assert
Assert.IsNotNull(result);
var home = result.ViewData.Model as HomeViewModel;
Assert.IsInstanceOf(typeof(HomeViewModel), home, "Wrong ViewModel");
Assert.AreEqual(3, home.Posts.Count());
//make sure the sort worked correctly
Assert.AreEqual(3, home.Posts.ElementAt(0).PostID);
Assert.AreEqual(2, home.Posts.ElementAt(1).PostID);
Assert.AreEqual(1, home.Posts.ElementAt(2).PostID);
}
但是,当我启动网站时,它没有返回任何记录(是的,实时数据库中有记录)。两种方式都将“page”变量设置为空。我发现如果我将“GetPaged”方法中的索引更改为 1 而不是 0 - 那么记录会在网站上返回......但是,一旦我这样做了 - 我的测试就不再通过了。我看到的所有文档都表明 GetPaged 索引是从零开始的......所以我在这里有点困惑。
有什么想法吗?
谢谢, 乍得
【问题讨论】:
标签: unit-testing subsonic subsonic3