我正在使用MongoMapper而不是ActiveRecord。rspec測試與多態類的問題
我有一個用戶模型和一個任務模型。 任務模型中,我有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 ==)
但是當我做在軌控制檯一樣,我取得好成績......
這裏是工廠:
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
泄漏規範似乎是完全不可能的(鏈接在ID上,該項目剛創建,...),並且'Factory.create(:task)'和'Factory(:task)'沒有區別' 。但是,應該確實是一個很好的提示! – nathanvda 2011-06-08 10:13:33
Thx爲答案,我使用mongo,我已經照顧應該,我不能用它來檢查關聯。但是,我會嘗試你說的其他事情。 – 2011-06-08 12:15:18
我已更新我的帖子,可以查看嗎? – 2011-06-08 12:25:08