Harish Ramachandran
Harish Ramachandran

Reputation: 293

Integration testing Devise using Test::Unit

I've installed and integrated Devise into my app and now I'm building its testing suite. I'm using the default testing suite (Test::Unit?) that comes with Rails 4.2.1 and am currently working on the integration suite.

INTEGRATION TEST:

  def setup
    @user = User.create(email: "[email protected]", 
                        encrypted_password: Devise.bcrypt(User, 'password1'))
    @post = { title: "This is the title",
               content: "Detailed comment."*10,
               phone: 9991118888,
               email: "[email protected]",
               user_id: users(:spiderman).id }
    @p = @user.posts.build(@post)
  end

  test "creates a new post successfully" do 
    sign_in_as(@user)
    get new_post_path(@user)
    assert_template 'posts/new'
    assert_difference 'Post.count', 1 do 
      post posts_path, post: @post
    end
    assert_template 'posts/show'
  end

I also created the following method in my test_helper.rb file:

  def sign_in_as(user)
     post_via_redirect user_session_path, 'user[:email]' => user.email, 
                           'user[:encrypted_password]' => Devise.bcrypt(User, 'password1')
  end

However, I get the following error when I run the test:

  1) Failure:
PostsCrudTest#test_creates_a_new_post_successfully [/Users/harishramachandran/dropbox/documents/harish/coding/workspace/h_list/test/integration/posts_crud_test.rb:19]:
expecting <"posts/new"> but rendering with <[]>

I looked for the solution online and all I've been able to find is solutions that involve RSpec or Capybara. This is an app I'm creating to learn, among other things, the default suite before I move on to RSpec and Capybara in a different app. Is there a way to resolve this issue in Test::Unit?

Upvotes: 2

Views: 2143

Answers (1)

Luis Menjivar
Luis Menjivar

Reputation: 777

After you create your post in post posts_path, post: @post you are not being redirected to the the post/show. Type follow_redirect! right after like so: post posts_path, post: @post follow_redirect! or alternatively you can do post_via_redirect posts_path, post: @post which will do the same. Post and then redirect to post/show.

A few notes Devise.bcrypt is deprecated as of a few days ago. So in the future if you update your Gemfile you will get an deprecation error. Yes you can do integration tests with Rails default testing. This is an example testing if the user gets the root path with or with out signing in.

require 'test_helper'

class UserFlowsTest < ActionDispatch::IntegrationTest
  test "user can see home page after login" do
    get user_session_path
    assert_equal 200, status
    @david = User.create(email: "[email protected]", password: Devise::Encryptor.digest(User, "helloworld"))
    post user_session_path, 'user[email]' => @david.email, 'user[password]' =>  @david.password
    follow_redirect!
    assert_equal 200, status
    assert_equal "/", path
  end

  test "user can not see home page without login" do
    get "/"
    assert_equal 302, status
    follow_redirect!
    assert_equal "/users/sign_in", path
    assert_equal 200, status
  end
end

Rails classes for testing (ActiveSupport::TestCase, ActionController::TestCase, ActionMailer::TestCase, ActionView::TestCase and ActionDispatch::IntegrationTest) inherit assertions from minitest assertions not test unit. This is true for your Rails version.

Upvotes: 6

Related Questions