I'm trying to learn how to test Rails controllers right now and I'm stuck. Whenever I try to test my test/controllers/articles_controller_test.rb file I get this error message.
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>'
The articles_controller_test.rb file is this:
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
I'm running Rails 4.1.10, and I'm pretty sure that my error has something to do with the Strong Params gem that Rails uses now. If someone could show me the problem with my code and explain why it's wrong that would be greatly appreciated.
This is the actual article_controller file, line 50 and line 67 are respectively outlined in stars:
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
Aucun commentaire:
Enregistrer un commentaire