RSpec 使用了错误的属性
RSpec using wrong attribute
RSpec 不是传递提交的 ID,而是传递提交的标题。有什么建议吗?
所有错误和文件都在这里:https://gist.github.com/maclover7/c6533d0b7e4cd7ad2322
错误信息
Failure/Error: put :update, hackathon_id: create(:hackathon), submission_id: @submission, submission: FactoryGirl.attributes_for(:submission), :format => :json, :access_token => @token.token
ActiveRecord::RecordNotFound:
Couldn't find Submission with 'id'=Dolorum ipsa officiis velit et id quo ea.
# ./app/controllers/api/v1/submissions_controller.rb:60:in `set_submission'
# ./spec/controllers/api/v1/submissions_controller_spec.rb:88:in `block (5 levels) in <top (required)>'
据此看来,set_submissionbefore_action好像有问题吧?但是,这是标准的 Rails 程序,所以我不确定这里的问题是什么。
submisisons_controller.rb
# Relevant action
private
def set_submission
@submission = Submission.find(params[:submission_id])
end
submissions_controller_spec.rb(相关部分)
describe "PUT #update" do
before :each do
@oauth_application = FactoryGirl.build(:oauth_application)
@token = Doorkeeper::AccessToken.create!(:application_id => @oauth_application.id, :resource_owner_id => user.id)
@submission = FactoryGirl.build(:submission)
end
context "valid attributes" do
it "located the requested @submission" do
put :update, hackathon_id: create(:hackathon), submission_id: @submission, submission: FactoryGirl.attributes_for(:submission), :format => :json, :access_token => @token.token
assigns(:submission).should eq(@submission)
end
it "changes @submission's attributes" do
put :update, hackathon_id: create(:hackathon), submission_id: @submission, submission: FactoryGirl.attributes_for(:submission, tagline: "lala"), :format => :json, :access_token => @token.token
@submission.reload
@submission.tagline.should eq("lala")
end
it "sends a 200 if updated submission if correct_user" do
if @submission.user_id == user.id
put :update, hackathon_id: create(:hackathon), submission_id: @submission, submission: FactoryGirl.attributes_for(:submission), :format => :json, :access_token => @token.token
response.status.should eq(200)
else
response.status.should eq(401)
end
end
end
已解决。需要确定
@submission = FactoryGirl.create(:submission)
,这会让你在提交时调用一个 id,submission_id: @submission.id
。
RSpec 不是传递提交的 ID,而是传递提交的标题。有什么建议吗?
所有错误和文件都在这里:https://gist.github.com/maclover7/c6533d0b7e4cd7ad2322
错误信息
Failure/Error: put :update, hackathon_id: create(:hackathon), submission_id: @submission, submission: FactoryGirl.attributes_for(:submission), :format => :json, :access_token => @token.token
ActiveRecord::RecordNotFound:
Couldn't find Submission with 'id'=Dolorum ipsa officiis velit et id quo ea.
# ./app/controllers/api/v1/submissions_controller.rb:60:in `set_submission'
# ./spec/controllers/api/v1/submissions_controller_spec.rb:88:in `block (5 levels) in <top (required)>'
据此看来,set_submissionbefore_action好像有问题吧?但是,这是标准的 Rails 程序,所以我不确定这里的问题是什么。
submisisons_controller.rb
# Relevant action
private
def set_submission
@submission = Submission.find(params[:submission_id])
end
submissions_controller_spec.rb(相关部分)
describe "PUT #update" do
before :each do
@oauth_application = FactoryGirl.build(:oauth_application)
@token = Doorkeeper::AccessToken.create!(:application_id => @oauth_application.id, :resource_owner_id => user.id)
@submission = FactoryGirl.build(:submission)
end
context "valid attributes" do
it "located the requested @submission" do
put :update, hackathon_id: create(:hackathon), submission_id: @submission, submission: FactoryGirl.attributes_for(:submission), :format => :json, :access_token => @token.token
assigns(:submission).should eq(@submission)
end
it "changes @submission's attributes" do
put :update, hackathon_id: create(:hackathon), submission_id: @submission, submission: FactoryGirl.attributes_for(:submission, tagline: "lala"), :format => :json, :access_token => @token.token
@submission.reload
@submission.tagline.should eq("lala")
end
it "sends a 200 if updated submission if correct_user" do
if @submission.user_id == user.id
put :update, hackathon_id: create(:hackathon), submission_id: @submission, submission: FactoryGirl.attributes_for(:submission), :format => :json, :access_token => @token.token
response.status.should eq(200)
else
response.status.should eq(401)
end
end
end
已解决。需要确定
@submission = FactoryGirl.create(:submission)
,这会让你在提交时调用一个 id,submission_id: @submission.id
。