SubSonic GetPaged 方法 - 单元测试中需要不同的索引

发布于 2024-08-05 19:50:10 字数 1599 浏览 4 评论 0原文

我正在使用 SubSonic 3,并使用 ActiveRecord 方法。我的控制器中有一个简单的查询:

var posts = from p in Post.GetPaged(page ?? 0, 20)
            orderby p.Published descending
            select p;

“page”变量被定义为可为 null 的 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”变量设置为 null。我发现如果我将“GetPaged”方法中的索引更改为 1 而不是 0 - 那么记录将返回到网站上...但是,一旦我这样做 - 我的测试就不再通过。我见过的所有文档都表明 GetPaged 索引是从零开始的......所以我在这里有点困惑。

有什么想法吗?

谢谢, 乍得

I'm using SubSonic 3, and am using the ActiveRecord approach. I have a simple query in my controller:

var posts = from p in Post.GetPaged(page ?? 0, 20)
            orderby p.Published descending
            select p;

The "page" variable is defined as an nullable int (int?).

Here's the problem, when I run the following test, it works fine and passes:

[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);
    }

However, when I launch the website, it doesn't return any records (and yes, there are records in the live database). Both ways have the "page" variable set to null. I found that if I change the index in "GetPaged" method to 1 instead of 0 - then records are returned on the website... however, as soon as I do that - my tests don't pass anymore. All the documentation I've seen shows that the GetPaged index is zero-based... so I'm a bit confused here.

Any ideas?

Thanks,
Chad

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

锦爱 2024-08-12 19:50:10

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

This seems to be a bug/inconsistency in the way GetPaged works with ActiveRecord. As you've already worked out it's using a 1 based index instead of a 0 based index in the ActiveRecord repository. Can you please log this as an issue at github

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文