使用多态类进行 rspec 测试时出现问题

发布于 2024-11-14 13:08:49 字数 1856 浏览 4 评论 0原文

我使用 MongoMapper 而不是 ActiveRecord。

我有一个用户模型和一个任务模型。 在任务模型中,我有以下 2 个属性:

  • 所有者
  • 作者

这两个属性都是用户引用。

以下是两个模型之间的关系:

User.rb

has_many :tasks, :as => :owner

Taks.rb

belongs_to :owner, :class_name => "User", :polymorphic => true

我使用 RSpec 来编写测试: (@user 之前已声明)

it "should have many tasks" do
    another_user = Factory.create(:user, :email => Faker::Internet.email)

    task_1 = Factory.create(:task, :owner => another_user, :author => another_user)
    task_2 = Factory.create(:task, :owner => another_user, :author => @user)
    task_3 = Factory.create(:task, :owner => @user, :author => @user)

    another_user.tasks.size.should == 2
end

这就是问题:

Failure/Error: another_user.tasks.size.should == 2
expected: 2
got: 3 (using ==)

但是,当我在 Rails 控制台中执行相同操作时,我得到了很好的结果...

这是工厂:

Factory.define :user do |u|
  u.first_name 'Test User'                          #
  u.username 'Test User'                            #
  u.surname 'TheTest'                               #
  u.email '[email protected]'                          #
  u.password 'please'                               #
  u.confirmed_at Time.now                           #
end

Factory.define :task do |u|
  u.author nil                                      #
  u.owner nil                                       #
  u.subjects []
  u.timeframe ""
  u.initially_placed_at nil
  u.label "Foo Task"                                #
  u.description "A small task description"
  u.done false
  u.pinned false
  u.confidentiality ""
end

I'm using MongoMapper instead of ActiveRecord.

I have a User model and a Task model.
In the Task model, I have 2 attributes as follow:

  • Owner
  • Author

Both attributes are User references.

Here are the relations between the two models:

User.rb

has_many :tasks, :as => :owner

Taks.rb

belongs_to :owner, :class_name => "User", :polymorphic => true

I've used RSpec for writing test:
(@user is declared before)

it "should have many tasks" do
    another_user = Factory.create(:user, :email => Faker::Internet.email)

    task_1 = Factory.create(:task, :owner => another_user, :author => another_user)
    task_2 = Factory.create(:task, :owner => another_user, :author => @user)
    task_3 = Factory.create(:task, :owner => @user, :author => @user)

    another_user.tasks.size.should == 2
end

And here is the issue:

Failure/Error: another_user.tasks.size.should == 2
expected: 2
got: 3 (using ==)

However when I do the same in rails console, I get good results...

Here are the factories:

Factory.define :user do |u|
  u.first_name 'Test User'                          #
  u.username 'Test User'                            #
  u.surname 'TheTest'                               #
  u.email '[email protected]'                          #
  u.password 'please'                               #
  u.confirmed_at Time.now                           #
end

Factory.define :task do |u|
  u.author nil                                      #
  u.owner nil                                       #
  u.subjects []
  u.timeframe ""
  u.initially_placed_at nil
  u.label "Foo Task"                                #
  u.description "A small task description"
  u.done false
  u.pinned false
  u.confidentiality ""
end

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

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

发布评论

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

评论(2

回忆躺在深渊里 2024-11-21 13:08:49

对此有几个答案:

1)您可能有一个在该规范之前运行的泄漏规范(这意味着在此之前使用“another_user”的 id 和类型创建了一个任务

2)可能有必要创建任务,太(尝试使用 Factory.create(:task) 而不是仅使用 Factory(:task)

3)您可能需要查看 shoulda,它可以帮助您非常轻松地指定关联,例如:

it { should have_many(:posts) }

There are several answers to this:

1) You may have a leaking spec that runs before this spec (this means that a task is created with the id and type of 'another_user' before this

2) It may be necessary to create the tasks, too (try to use Factory.create(:task) instead of just Factory(:task)

3) You may want to check out shoulda, it helps you spec associations very easily, like this for example:

it { should have_many(:posts) }

谁与争疯 2024-11-21 13:08:49

这确实是相当令人麻木的。我看到一些选项可以帮助您找到这一点:

  • 使用 Rubymine,您可以在其中轻松调试测试

  • 添加大量日志语句

    p>

  • 添加以下测试:

    another_user.tasks.should =~ [task1, task2]

这将显示项目列表中的差异,并且将其他项目是task3

  • 我希望你们工厂不要为每个用户创建默认任务?

除此之外:确实,请查看soulda

That is pretty mind-numbing indeed. I see a few options to help you find this:

  • use Rubymine, where you can easily debug your tests

  • add a lot of log-statements

  • add the following test:

    another_user.tasks.should =~ [task1, task2]

This will show the difference in the list of items, and will the other item be task3 ?

  • i hope your factory does not create a default task for each user?

Aside of that: indeed, checkout shoulda!

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