Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rspec关联测试失败_Ruby On Rails_Rspec - Fatal编程技术网

Ruby on rails rspec关联测试失败

Ruby on rails rspec关联测试失败,ruby-on-rails,rspec,Ruby On Rails,Rspec,对rspec来说还是有点陌生,无法通过以下测试(问题区域“it”应该按照正确的顺序“do”块进行正确的处理): 用户_spec.rb describe User do before do @user = User.new(name: "Example User", email: "user@example.com", password: "foobar", password_confirmation: "foobar")

对rspec来说还是有点陌生,无法通过以下测试(问题区域“it”应该按照正确的顺序“do”块进行正确的处理):

用户_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
在适当的代码后面寻找逻辑解释来填充“it”,应该在用户规范测试的正确顺序“do”块中进行正确的处理。谢谢

编辑:对不起,忘记了错误消息,这里是:

失败:

1) 用户治疗协会应该按照正确的顺序进行正确的治疗 失败/错误:FactoryGirl.create(:treating,user:@user,created_于:1.天前) 命名错误: 未定义的方法<代码>用户='#
#./spec/models/user_spec.rb:143:在“

中的块(3级)中,您试图覆盖一个不存在的字段

您没有用户,只有请求者或请求者。 举个例子

FactoryGirl.create(:treating, requestor: @user, created_at: 1.hour.ago)

谢谢我试过了,但收到了以下错误:3)用户治疗协会应销毁相关治疗失败/错误:FactoryGirl.create(:requestor,User:@User,created_at:1.day.ago)ArgumentError:工厂未注册:requestor您是否试图创建用户并请求请求者。请求者未在工厂中定义,因此未注册。抱歉,我的意思是:FactoryGirl.create(:treating,请求者:@user,created_at:1.day.ago)不幸的是,使用我的用户规范中的上述代码,在我的终端中仍然收到关于未注册的“trait”的类似错误消息:1)用户处理关联应按正确顺序进行正确处理失败/错误:FactoryGirl.create(:treating,requestor:@User,created_at:1.day.ago)ArgumentError:trait未注册:requestor
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
FactoryGirl.create(:treating, requestor: @user, created_at: 1.hour.ago)