While following http://guides.rubyonrails.org/testing.html and trying to run test against my auto-generated controller this is the error that I am facing.
Both the controller and the test file for the controller are auto generated. Only change was made to the model by making the title
field mandatory.
Not sure what I am missing. Does the rails server needs to be up and running in order to run the functional tests?
Error Log
> rake test test/controllers/articles_controller_test.rb
Run options: --seed 43757
# Running:
E....
Finished in 0.467545s, 10.6942 runs/s, 10.6942 assertions/s.
1) Error:
ArticlesControllerTest#test_should_create_article:
ActionController::InvalidAuthenticityToken: ActionController::InvalidAuthenticityToken
test/controllers/articles_controller_test.rb:12:in `block (2 levels) in <class:ArticlesControllerTest>'
test/controllers/articles_controller_test.rb:10:in `block in <class:ArticlesControllerTest>'
5 runs, 5 assertions, 0 failures, 1 errors, 0 skips
articles_controller_test.rb
require 'test_helper'
class ArticlesControllerTest < ActionController::TestCase
setup do
@article = articles(:one)
end
test "should create article" do
assert_difference('Article.count') do
post :create, article: {body: @article.body, title: @article.title}
end
assert_redirected_to article_path(assigns(:article))
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:articles)
end
..
...
end
article.rb
class Article < ActiveRecord::Base
validates :title, presence: true
end
articles.yml
one:
title: MyString
body: MyText
two:
title: MyString
body: MyText
articles_controller.rb
class ArticlesController < ApplicationController
before_action :set_article, only: [:show, :edit, :update, :destroy]
# GET /articles
# GET /articles.json
def index
@articles = Article.all
end
# GET /articles/1
# GET /articles/1.json
def show
end
# GET /articles/new
def new
@article = Article.new
end
# GET /articles/1/edit
def edit
end
# POST /articles
# POST /articles.json
def create
@article = Article.new(article_params)
respond_to do |format|
if @article.save
format.html { redirect_to @article, notice: 'Article was successfully created.' }
format.json { render :show, status: :created, location: @article }
else
format.html { render :new }
format.json { render json: @article.errors, status: :unprocessable_entity }
end
end
end
..
....
end