【发布时间】:2016-04-03 15:28:47
【问题描述】:
我现在正在尝试学习如何测试 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
【问题讨论】:
-
您的控制器看起来如何(尤其是
update中的行67- 请参阅错误消息以了解我询问的原因)?在你的测试中50是什么线(我只是不想数......),你能标记那条线吗? -
你试过 put :update, :id => @article.id 吗?
-
@margo 是的,我刚试过,同样的错误信息出现了
-
@spickermann 我用星星勾勒出第 50 行,第 67 行是最后一行,这对我来说也没有多大意义
-
能否请您发布您的
ArticlesController。该文件有问题,尤其是在行号混乱的情况下。
标签: ruby-on-rails ruby testing controller