Ziyu
Ziyu

Reputation: 1127

rails integration tests pass with ruby -Itest, fail with rake test:integration

I'm following the rails_admin README. I have setup devise and cancan, only users of group admin can access rails_admin. Here is the test

test "try to access rails_admin as a non admin user" do
  get_via_redirect '/users/auth/facebook'
  assert_response :success
  assert_match 'Successfully authenticated', flash[:notice]

  puts User.all.count
  puts User.first.name
  puts Group.find(User.first.group_id).name
  assert_equal Ability.new(User.first).can?(:access, :dashboard), false
  puts session

  get '/admin'
  assert_response :found
  assert_redirected_to '/'
  assert_match 'You are not authorized', flash[:alert]
end

When I run 'ruby -Itest test/integration/test.rb' all passes. The output is

1
Facebook User
user
{"session_id"=>"5345e64582b2557d0d02cd2011461467", "warden.user.user.key"=>["User", [2], "$2a$04$B.nVokuCXSWOpZ2Ezf60Cu"], "flash"=>#<ActionDispatch::Flash::FlashHash:0xb49d9a4 @used=#<Set: {:notice}>, @closed=false, @flashes={:notice=>"Successfully authenticated from facebook account."}, @now=nil>}

When I run 'bundle exec rake test:integration' the test fails. The output is

1
Facebook User
user
{"session_id"=>"0005d96c17c75d0843166e5dbb4dcc05", "warden.user.user.key"=>["User", [3], "$2a$04$4f5/I9uSZbMWBdCgDA086O"], "flash"=>#<ActionDispatch::Flash::FlashHash:0xa4c0db0 @used=#<Set: {:notice}>, @closed=false, @flashes={:notice=>"Successfully authenticated from facebook account."}, @now=nil>}
F.

Finished tests in 1.054687s, 3.7926 tests/s, 24.6519 assertions/s.

1) Failure:
test_try_to_access_rails_admin_as_a_non_admin_user(RailsAdminTest):
Expected response to be a <:found>, but was <200>

I also tried "assert_select 'body', 'something'". When running rake test:integration it outputs some html like a standard rails_admin dashborad page. Looks like the user is authorized to access rails_admin.

I'm using rails 3.2.6, devise 2.1.2, cancan 1.6.8, rails_admin 0.0.5

Any ideas? Thanks.

Upvotes: 3

Views: 417

Answers (1)

Michael
Michael

Reputation: 136

I was having the same issues (single test running but not all at once). It seems this is due to rails_admin not being initalized by default when running test. What helped was setting

SKIP_RAILS_ADMIN_INITIALIZER=false

before calling rake. So now it's

SKIP_RAILS_ADMIN_INITIALIZER=false rake test

for me.

Unfortunately i do not know how to reinitialize rails_admin from inside the tests (there is a rake task to disable but not to enable) and prefixing rake test every time is annoying. Also the tests do run quite a bit longer if rails_admin is initalized (that's propably why it's turned of by default).

We now use a small test helper for those tests which check rails_admin access. The helper returns immediately and logs a warning if SKIP_RAILS_ADMIN_INITIALIZER is true and thus does not run the tests on normal dev systems. On our continuous integration system SKIP_RAILS_ADMIN_INITIALIZER=false is set and the tests are run.

Upvotes: 1

Related Questions