rspecはまだ少し新しく、次のテストに合格することができません(問題領域「適切な処理を適切な順序で行う必要があります」ブロック)。
user_spec.rb
describe User do
before do
@user = User.new(name: "Example User", email: "user@example.com",
password: "foobar", password_confirmation: "foobar")
end
describe "treating associations" do
before { @user.save }
let!(:older_treating) do
FactoryGirl.create(:treating, user: @user, created_at: 1.day.ago)
end
let!(:newer_treating) do
FactoryGirl.create(:treating, user: @user, created_at: 1.hour.ago)
end
it "should have the right treatings in the right order" do
@user.sent_treatings.should == [newer_treating, older_treating]
@user.received_treatings.should == [newer_treating, older_treating]
end
end
end
以下のユーザーモデルと治療モデルに基づいて、テストのどこかに「リクエスター」と「リクエスティ」を埋め込む必要があることを知っています。さまざまなバリエーションを試しましたが、すべて失敗し続けています。モデルは次のとおりです。
user.rb
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
has_many :sent_treatings, :foreign_key => "requestor_id", :class_name => "Treating"
has_many :received_treatings, :foreign_key => "requestee_id", :class_name => "Treating"
end
治療.rb
class Treating < ActiveRecord::Base
attr_accessible :intro, :proposed_date, :proposed_location
validates :requestor_id, presence: true
validates :requestee_id, presence: true
belongs_to :requestor, class_name: "User"
belongs_to :requestee, class_name: "User"
default_scope order: 'treatings.created_at DESC'
end
これが私のfactories.rbファイルです:
factorys.rb
FactoryGirl.define do
factory :user do
sequence(:name) { |n| "Person #{n}" }
sequence(:email) { |n| "person_#{n}@example.com"}
password "foobar"
password_confirmation "foobar"
factory :admin do
admin true
end
end
factory :treating do
intro "Lorem ipsum"
user
end
end
user_specテストの「適切な処理を適切な順序で行う必要がある」do'ブロックに入力する適切なコードの背後にあるロジックの説明を探します。ありがとう!
編集:申し訳ありませんが、エラーメッセージを忘れました、ここにあります:
失敗:
1)ユーザー処理アソシエーションは、正しい順序で正しい処理を行う必要があります失敗/エラー:FactoryGirl.create(:treating、user:@user、created_at:1.day.ago)NoMethodError:未定義のメソッドuser=' for #<Treating:0x0000010385ec70>
# ./spec/models/user_spec.rb:143:in
ブロック(3レベル)'