使用多态类进行 rspec 测试时出现问题
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对此有几个答案:
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) }
这确实是相当令人麻木的。我看到一些选项可以帮助您找到这一点:
使用 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
?Aside of that: indeed, checkout shoulda!