传递无效参数时Rspec是错误的

时间:2013-09-09 01:36:53

标签: ruby-on-rails rspec

我为创建动作创建一个Rspec控制器测试,该动作获取两个对象,故事和ku的参数。故事验证了title和large_cover_url的存在。不确定为什么rspec在传递无效参数时会引发参数错误,并且可以对此问题使用一些见解。

这是错误:

Failure/Error: post :create, story: {title: nil, large_cover_url: "present"}, ku: Fabricate.attributes_for(:ku)
     ArgumentError:
       wrong number of arguments (0 for 2)
     # ./app/controllers/stories_controller.rb:18:in `create'
     # ./spec/controllers/stories_controller_spec.rb:59:in `block (4 levels) in <top (required)>'

以下是规范:

context "failed story creation" do
    let(:bob) { Fabricate(:user) }
    before { sign_in_user(bob) }

    it "does not create a new story" do
        post :create, story: {title: nil, large_cover_url: "present"}, ku: Fabricate.attributes_for(:ku)
        expect(Story.count).to eq(0)
    end
end

以下是控制器操作:

def create
    @story = Story.new(params[:story])
    @ku = Ku.new(params[:ku])
  if @story.save && @ku.save
    @story.update_column(:user_id, current_user.id)
    @ku.update_column(:user_id, current_user.id)
    @story.kus << @ku
    redirect_to story_path(@story), flash: {success: "Your story was published."}
  else
    flash[:error] = "#{@story.errors.full_message.join(', ')}"
    render :new
  end
end

1 个答案:

答案 0 :(得分:1)

方法full_message接受属性名称和错误字符串作为参数,因此当您执行flash[:error] = "#{@story.errors.full_message.join(', ')}"时,您正在调用full_message而没有任何参数。 您应该使用full_messages来返回包含所有错误消息的数组。

ActiveModel::Errors api ref

相关问题