vendredi 24 juin 2016

Cannot retrieve saved data from activerecord while testing

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

Aucun commentaire:

Enregistrer un commentaire