-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Rake Testing
Daniel Weller edited this page Nov 29, 2015
·
1 revision
This assumes you have set up tags using Tutorial for Posts
To test using rake
you can do the following:
Ensure that you are using acts_as_taggable_on :tags
instead of acts_as_taggable
in app/models/post.rb or your tests will fail. Reference: http://stackoverflow.com/questions/18725506/acts-as-taggable-on
class Post < ActiveRecord::Base
acts_as_taggable_on :tags
end
tests/controllers/posts_controller_test.rb
class PostsControllerTest < ActionController::TestCase
setup do
@post = posts(:one)
@tag = ActsAsTaggableOn::Tag.find(1)
end
...
test "should create post" do
assert_difference('Post.count') do
post :create, post: { content: @post.content, title: @post.title, tag_list: @tag.name }
end
assert_redirected_to post_path(assigns(:post))
end
test "should update post" do
patch :update, id: @post, post: { content: @post.content, title: @post.title, tag_list: @tag.name }
assert_redirected_to post_path(assigns(:post))
end
end
Here we add tag_list: @tag.name
into the :create
& update
actions for should create post
& should update post
. We define @tag
by looking for the id of 1 from fixtures/tags.yml.
tests/fixtures/tags.yml
one:
id: 1
name: animals
You may want to create tests for your tag views too. You can do that by creating
tests/controllers/tags_controller_test.rb
require 'test_helper'
class TagsControllerTest < ActionController::TestCase
setup do
@tag = ActsAsTaggableOn::Tag.find(1)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:tags)
end
test "should get show" do
get :show, id: @tag
assert_response :success
end
end
Running rake test
should now pass