This is the test I wrote. You’ll notice it is almost verbatim the scaffolded test.

1
2
3
4
5
6
7
8
9
10
def test_create
  num_blog_accounts = BlogAccount.count

  post :new,
    :blog_account => {:feed_url => 'http://bladibla.com/'},
    :commit => 'Close'
  assert_redirected_to blog_accounts_url

  assert_equal num_blog_accounts + 1, BlogAccount.count
end

Interestingly, I made it fail with the following simple code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
def new
  return edit unless params[:id].blank?
  @title = 'Creating blog account'
  @blog_account = BlogAccount.new
  save_and_navigate if request.post?
end

protected
def save_and_navigate
  if @blog_account.update_attributes(params[:blog_account])
    flash_success 'BlogAccount was successfully updated.'
    respond_to do |wants|
      wants.js do
        render :action => 'save', :content_type => 'text/javascript; charset=utf-8', :layout => false
      end
      wants.html do
        redirect_to blog_accounts_url
      end
    end
  end
end
The failure was:
1
2
3
4
5
6
7
8
9
10
11
$ ruby test\functional\admin\blog_accounts_controller_test.rb -n test_create
Loaded suite test/functional/admin/blog_accounts_controller_test
Started
F
Finished in 0.188 seconds.

  1) Failure:
test_create(Admin::BlogAccountsControllerTest) [test/functional/admin/blog_accounts_controller_test.rb:42]:
Expected response to be a <:redirect>, but was <200>

1 tests, 1 assertions, 1 failures, 0 errors
Flipping the order of my respond_to block did the trick:
1
2
3
4
5
6
7
8
respond_to do |wants|
  wants.html do
    redirect_to blog_accounts_url
  end
  wants.js do
    render :action => 'save', :content_type => 'text/javascript; charset=utf-8', :layout => false
  end
end

This is partially logical. Of course, the request doesn’t have an Accept header, so Rails uses the default, which is to return in order the order of definition.

Alternatively, setting the request’s Accept header would do the trick:

1
2
3
4
5
6
7
def setup
  @controller = Admin::BlogAccountsController.new
  @request    = ActionController::TestRequest.new
  @response   = ActionController::TestResponse.new

  @request.env['HTTP_ACCEPT'] = 'text/html'
end

Just had a bit of a problem today. I wanted to test access to some actions, but with cookies. I had to dig pretty deeply before I found what I needed.

When setting your cookies in the test, use CGI::Session, and not a pure Hash:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class AccessDownloadsByCookieTest < Test::Unit::TestCase
  def setup
    @controller = AttachmentsController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new

    # WARNING: this is easy to fake, so don't do that
    # if you require real security
    @request.cookies['auth'] = CGI::Cookie.new('auth', '1')
    get :download
  end

  def test_access_granted_to_resource_by_cookie
    assert_response :success
  end
end

 

Search

A picture of me

I am François Beausoleil, a Ruby on Rails coder. During the day, I work on XLsuite. At night, I am interested many things. Read my biography

Tags

(3) (1) (0) (2) (1) (1) (2) (2) (1) (2) (1) (2) (1) (2) (1) (1) (1) (1) (2) (14) (1) (1) (1) (1) (2) (1) (1) (2) (0) (1) (2) (1) (3) (1) (1) (1) (1) (1) (1) (0) (3) (2) (1) (2) (2) (1) (3) (2) (8) (8) (9) (12) (1) (1) (3) (1) (1) (1) (1) (1) (1) (2) (2) (2) (1) (1) (3) (1) (3) (1) (0) (23) (1) (1) (0) (1) (1) (1) (23) (25) (1) (1) (13) (1) (1) (2) (3) (1) (1) (4) (1) (2) (3) (0) (1) (7) (3) (1) (5) (5) (2) (2) (2) (4) (6) (7) (1) (0) (1) (1) (2) (2) (1) (4) (12) (2) (1) (2) (4) (1) (1) (1) (2) (8) (2) (3) (2) (2) (1) (3) (1) (1)

Links

Projects I work on

Categories

Archives