0
我使用SubSonic 3,並使用ActiveRecord方法。我在我的控制器中有一個簡單的查詢:SubSonic GetPaged方法 - 單元測試中需要的不同索引
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);
}
然而,當我推出的網站,它不返回任何記錄(是的,有記錄在實時數據庫中)。兩種方法都將「頁面」變量設置爲空。我發現如果我將「GetPaged」方法中的索引更改爲1而不是0,那麼記錄會在網站上返回......但是,只要我這樣做 - 我的測試就不會再通過了。我見過的所有文檔都顯示GetPaged索引是基於零的......所以我在這裏有點困惑。
任何想法?
感謝, 乍得