【问题标题】:SubSonic GetPaged method - different index needed in unit testSubSonic GetPaged 方法 - 单元测试中需要不同的索引
【发布时间】: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


    【解决方案1】:

    这似乎是 GetPaged 与 ActiveRecord 的工作方式中的错误/不一致。正如您已经计算出的那样,它在 ActiveRecord 存储库中使用基于 1 的索引而不是基于 0 的索引。您能否将此作为问题记录在github

    【讨论】:

      猜你喜欢
      • 2010-11-28
      • 2019-04-04
      • 2014-05-19
      • 2021-11-16
      • 1970-01-01
      • 2012-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多