我无法弄清楚为什么RSpec会出错。 错误信息是风箱。
1) ArticlesController POST #update with valid params assigns the article as @article
Failure/Error: @article = Article.find(params[:article][:id])
ActiveRecord::RecordNotFound:
Couldn't find Article with 'id'=
# ./app/controllers/articles_controller.rb:205:in `update'
# ./spec/controllers/articles_controller_spec.rb:121:in `block (4 levels) in <top (required)>'
我为RSpec测试准备了数据库。架构已经形成,数据是空的。
这是我的文章控制器
def update
@article = Article.find(params[:article][:id])
@article.publish_at = params[:article][:publish_at]
@article.publish_until = params[:article][:publish_until]
@article.update_user_id = params[:article][:update_user_id]
@article.article_histories.build
@article.article_histories.last.article_id = params[:article][:id]
@article.article_histories.last.title = params[:article_histories][:title]
@article.article_histories.last.content = params[:article_histories][:content]
@article.article_histories.last.version = @article.article_histories.maximum(:version) + 1
@article.current_version = @article.article_histories.last.version
@article.status = Article.statuses[:ready] unless @article.scrape?
@article.thumbnail = article_params[:file]
我的RSpec测试是这样的。
describe 'POST #update' do
context 'with valid params' do
let(:new_attributes) {
{
current_version: 2,
publish_version: 2,
thumbnail: "uploads/article/thumb/thumb_20170327055055.png",
status: "publish",
publish_datetime: "2017-04-16 19:51:03",
user_id: 2,
update_user_id: 3,
article_histories_attributes:[
version: 2,
title: "Hello! again",
content: "World!World!!! <br />"
]
}
}
it 'assigns the article as @article' do
article = Article.create! valid_attributes
put :update, params: {id: article.to_param, article: new_attributes}, session: valid_session
expect(assigns(:article)).to eq(article)
end
我该怎么做才能解决问题?
[更新] 我修改了一些像这样的参数。但仍然无效。
RSpec.describe ArticlesController, type: :controller do
login_admin
let(:valid_attributes) {
{
id: 1,
current_version: 1,
publish_version: 1,
thumbnail: "uploads/article/thumb/thumb_20170327055055.png",
status: "publish",
publish_datetime: "2017-04-16 19:51:03",
user_id: 1,
update_user_id: 1,
article_histories_attributes:[
version: 1,
title: "Hello!",
content: "World!World!!!"
]
}
}
describe 'POST #update' do
context 'with valid params' do
let(:new_attributes) {
{
id: 1,
current_version: 2,
publish_version: 2,
thumbnail: "uploads/article/thumb/thumb_20170327055055.png",
status: "publish",
publish_datetime: "2017-04-16 19:51:03",
user_id: 2,
update_user_id: 3,
article_histories_attributes:[
version: 2,
title: "Hello! again",
content: "World!World!!! <br />"
]
}
}
答案 0 :(得分:0)
该行为您提供ActiveRecord::RecordNotFound
例外
@article = Article.find(params[:article][:id])
id
内的哈希中没有密钥article
。
答案 1 :(得分:0)
你试图找到params [:article] [:id],但它不在你的哈希中。在您的测试中,您发送的id可以通过params [:id]访问它。如果你的控制器中有自定义路由,你应该这样做:
class ArticlesController < ApplicationController
before_action set_article, only: [:show, :edit, :update, :destroy, :custom_route]
# your actions goes here
# ...
protected
def set_article
@article ||= Article.find(params[:id] || params[:article][:id])
end
end