Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

ruby - How can I test ActiveRecord::RecordNotFound in my rails app?

I have this code in my controller and want to test this code line with a functional test.

raise ActiveRecord::RecordNotFound if @post.nil?

which assert method should I use? I use the built-in rails 2.3.5 test framework.

I tried it with this code:

  test "should return 404 if page doesn't exist." do
    get :show, :url => ["nothing", "here"]
    assert_response :missing
  end

but it doesn't work for me. Got this test output:

test_should_return_404_if_page_doesn't_exist.(PageControllerTest):
ActiveRecord::RecordNotFound: ActiveRecord::RecordNotFound
app/controllers/page_controller.rb:7:in `show'
/test/functional/page_controller_test.rb:21:in `test_should_return_404_if_page_doesn't_exist.'
question from:https://stackoverflow.com/questions/2492276/how-can-i-test-activerecordrecordnotfound-in-my-rails-app

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are two things you can do. The first is to let ActionController provide the default action when it rescues ActiveRecord::RecordNotFound:

class PostsControllerTest < ActionController::TestCase
  test "raises RecordNotFound when not found" do
    assert_raises(ActiveRecord::RecordNotFound) do
      get :show, :id => 1234
    end
  end
end

Using this method, you can't assert what gets rendered. You have to trust Rails/ActionController not to change behaviour.

An alternative, which I've sometimes used, is this:

class PostsControllerTest < ActionController::TestCase
  test "renders post_missing page, and returns 404" do
    get :show, params: { :id => 1234 }

    assert_response :not_found
    assert_template "post_missing"
  end
end

class PostsController < ApplicationController
  def show
    @post = current_user.posts.find_by!(slug: params[:slug])
  end

  rescue_from ActiveRecord::RecordNotFound do
    render :action => "post_missing", :status => :not_found
  end
end

You should read more about #rescue_from on the ActiveSupport API.

For simplicity, I usually go with my 1st solution though.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...