【问题标题】:Rspec: wrong number of arguments (1 for 0)Rspec:参数数量错误(1 代表 0)
【发布时间】:2015-08-06 10:06:25
【问题描述】:

这是一段代码,它会在第 5 行引发错误“args 数量错误(1 对 0):

describe 'PATCH #update' do
  context 'title is empty' do
    it 'should not update page' do
      page = create(:page)
      patch :update, { id: page.id, page: FactoryGirl.attributes_for(:page, title: nil) }, format: :json
      expect(response).to have_http_status(:unprocessable_entity)
    end
  end
end

Rails 控制器:

def update
    if @page.update(page_params)
      render json: @page
    else
      status :unprocessable_entity
    end
  end

其他测试工作得很好,但这对于#update's else - 没有。 无法理解,这是什么问题,我该如何解决? (功能正常,但测试失败)

【问题讨论】:

  • 你有什么问题?
  • 如何解决这个错误,这很明显。
  • status :unprocessable_entity不应该是render json: @page.errors, status: :unprocessable_entity吗?
  • @Pavan,你说得对。谢谢,将其发布为答案,它有效。

标签: ruby-on-rails ruby rspec


【解决方案1】:

您需要将update 方法更改为以下

def update
 if @page.update(page_params)
   render json: @page
 else
   render json: @page.errors, status: :unprocessable_entity
 end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多