我正在尝试学习如何测试Rails控制器,而且我被卡住了。每当我尝试测试我的test / controllers / articles_controller_test.rb文件时,都会收到此错误消息。
ArticlesControllerTest#test_should_update_article:
TypeError: can't cast ActionController::Parameters to integer
app/controllers/articles_controller.rb:67:in `update'
test/controllers/articles_controller_test.rb:50:in `block in <class:ArticlesControllerTest>'
articles_controller_test.rb文件是:
require 'test_helper'
class ArticlesControllerTest < ActionController::TestCase
setup do
@article = articles(:welcome_to_rails)
end
test "should get index" do
get :index
assert_response :success
assert_template 'index'
assert_not_nil assigns(:articles)
end
test "should get new" do
login_as(:eugene)
get :new
assert_response :success
end
test "should create article" do
login_as(:eugene)
assert_difference('Article.count') do
post :create, :article => { :title => 'Post title',
:body => 'Lorem ipsum..' }
end
assert_response :redirect
assert_redirected_to article_path(assigns(:article))
end
test "should show article" do
get :show, :id => @article.to_param
assert_response :success
assert_template 'show'
assert_not_nil assigns(:article)
assert assigns(:article).valid?
end
test "should get edit" do
login_as(:eugene)
get :edit, :id => @article.to_param
assert_response :success
end
test "should update article" do
login_as(:eugene)
**put :update, :id => @article.to_param, :article => { :title => 'New Title' }**
assert_redirected_to article_path(assigns(:article))
end
test "should destroy article" do
login_as(:eugene)
assert_nothing_raised { Article.find(@article.to_param)}
assert_difference('Article.count', -1) do
delete :destroy, :id => @article.to_param
end
assert_response :redirect
assert_redirected_to articles_path
assert_raise(ActiveRecord::RecordNotFound) { Article.find(@article.to_param) }
end
end
我正在运行Rails 4.1.10,而且我很确定我的错误与Rails现在使用的Strong Params gem有关。如果有人能够向我展示我的代码的问题并解释为什么它会出错,那将非常感激。
这是实际的article_controller文件,第50行和第67行分别用星号概述:
class ArticlesController < ApplicationController
before_filter :authenticate, :except => [:index, :show]
def index
@articles = Article.all
respond_to do |format|
format.html
format.xml { render :xml => @articles }
end end
def show
@article = Article.find(params[:id])
respond_to do |format|
format.html
format.xml { render :xml => @article }
end
end
def new
@article = Article.new
respond_to do |format|
format.html
format.xml { render :xml => @article }
end
end
def edit
@article = current_user.articles.find(params[:id])
end
def create
@article = current_user.articles.new(article_params)
respond_to do |format|
if @article.save
format.html { redirect_to(@article, :notice => 'Article was successfully created.') }
**format.xml { render :xml => @article, :status => :created, :location => @article }**
else
format.html { render :action => "new" }
format.xml { render :xml => @article.errors, :status => :unprocessable_entity }
end
end
end
def notify_friend
@article = Article.find(params[:id])
Notifier.email_friend(@article, params[:name], params[:email]).deliver
redirect_to @article, :notice => "The message has been sent to your friend"
end
def update
**@article = current_user.articles.find(article_params)**
respond_to do |format|
if @article.update_attributes(params[:article])
format.html { redirect_to(@article, :notice => 'Article was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @article.errors, :status => :unprocessable_entity }
end
end
end
def destroy
@article = current_user.articles.find(params[:id])
@article.destroy
respond_to do |format|
format.html { redirect_to(articles_url) }
format.xml { head :ok }
end
end
end
def article_params
params.require(:article).permit(:title, :location, :categories, :excerpt, :body, :published_at)
end
答案 0 :(得分:0)
您似乎在课堂外定义了article_params
方法。只需更改文件中的最后五行
end
def article_params
params.require(:article).permit(:title, :location, :categories, :excerpt, :body, :published_at)
end
到
def article_params
params.require(:article).permit(:title, :location, :categories, :excerpt, :body, :published_at)
end
end
此外,我认为您应该在控制器中更改此行
def create
@article = current_user.articles.new(article_params)
到
def create
@article = current_user.articles.build(article_params)
查看has_many
方法向类添加的方法:返回新记录的方法称为build
,而不是new
...
答案 1 :(得分:0)
班级结束了错误的地方。将它移到article_params方法定义之前。