0

I am testing a controller. Index just method saves article to activerecord but i cannot get the article from test code.

What am I missing?

Controller

class ArticlesController < ApplicationController
  def create

    if Article.new(:title => "abc").save
        render status: 200, json: ""
    else 
        render status: 500, json: ""
    end

  end
end

Test

require 'test_helper'

class ArticlesControllerTest < ActionController::TestCase
  test "should get create" do
    get :create
    assert_response :success
    assert_nil Article.where(title: "abc"), "Article nil"

  end

end

I get following result

F

Finished in 0.100930s, 9.9079 runs/s, 19.8157 assertions/s.

  1) Failure:
ArticlesControllerTest#test_should_get_index [test/controllers/articles_controller_test.rb:7]:
Article nil.
Expected #<ActiveRecord::Relation [#<Article id: 980190963, title: "abc", created_at: "2016-06-24 13:23:36", updated_at: "2016-06-24 13:23:36">]> to be nil.

1 runs, 2 assertions, 1 failures, 0 errors, 0 skips
7
  • I shouldn't have used "index" for example, this should be "create" or something but I think the point of the question is clear. Commented Jun 24, 2016 at 13:32
  • I edited "index" to "create" Commented Jun 24, 2016 at 13:34
  • You can't use development database to test environment directly. you need create a database for test environment first, than insert data into database at the time of running the test case. Commented Jun 24, 2016 at 13:36
  • What do you mean by "create a database for test environment"? I ran "rake db:test:prepare" which makes no change. Commented Jun 24, 2016 at 13:52
  • rake:db:test:prepare is deprecated. Commented Jun 24, 2016 at 14:07

2 Answers 2

1

You are actually receiving the created Article record. Look at the last line of your test output. It says "Expected #ActiveRecord...", which means it returned an Article object but it was supposed to not return anything (nil).

The problem with you test code is your assertion line. assert_nil is the wrong method to use in this case.

Try something like:

assert_equal "abc", Article.first.title
Sign up to request clarification or add additional context in comments.

2 Comments

I am really sorry but I was pasting wrong error message, I replaced it by correct one. I think the problem is I haven't created a test database
The code I pasted wasn't what i meant to show but I realized that I needed to add ".first" after I got object by where method.
1

Just look at your code and your test.

You have a GET#create call, where you do indeed create an Article object with the title 'abc'.

Then in your test, you call this action, which will create the Article with 'abc' as title, and then do this assertion:

 assert_nil Article.where(title: "abc"), "Article nil"

Which fails, and it should, because there is an Article with 'abc' as a title (You just created it in you controller).

What your're doing is the wrong assertion. You don't want to assert_nil the article, you want to make sure that he is there, hence not nil.

Something like:

class ArticlesControllerTest < ActionController::TestCase
  test "should get create" do
    get :create
    assert_response :success
    refute_nil Article.where(title: "abc").first, "Article should not be nil"
  end
end

Aside this problem Article.where will not return you nil if it finds no article. It will give you an empty ActiveRecord::Relation (#<ActiveRecord::Relation []>)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.