3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RailsチュートリアルでMicropostのテストでエラーが出る

Last updated at Posted at 2018-06-26

Railsチュートリアルの第2章終了時点でテストを実行するとエラーが発生したので、なぜだか調べてみました。

実行結果は以下のとおり。2つのfailuresが発生しています。

テストNG
C:\Users\ror\sampleapp>rails t
Run options: --seed 47196

# Running:

..F

Failure:
MicropostsControllerTest#test_should_create_micropost [C:/Users/ror/sampleapp/test/controllers/microposts_controller_test.rb:19]:
"Micropost.count" didn't change by 1.
Expected: 3
  Actual: 2


bin/rails test test/controllers/microposts_controller_test.rb:18

.F

Failure:
MicropostsControllerTest#test_should_update_micropost [C:/Users/ror/sampleapp/test/controllers/microposts_controller_test.rb:38]:
Expected response to be a <3XX: redirect>, but was a <200: OK>


bin/rails test test/controllers/microposts_controller_test.rb:36

.........

Finished in 12.588715s, 1.1121 runs/s, 1.3504 assertions/s.
14 runs, 17 assertions, 2 failures, 0 errors, 0 skips

そのときのソースは以下。

test\controllers\microposts_controller_test.rb
require 'test_helper'

class MicropostsControllerTest < ActionDispatch::IntegrationTest
  setup do
    @micropost = microposts(:one)
  end

  test "should get index" do
    get microposts_url
    assert_response :success
  end

  test "should get new" do
    get new_micropost_url
    assert_response :success
  end

  test "should create micropost" do
    assert_difference('Micropost.count') do
      post microposts_url, params: { micropost: { content: @micropost.content, user_id: @micropost.user_id } }
    end

    assert_redirected_to micropost_url(Micropost.last)
  end

  test "should show micropost" do
    get micropost_url(@micropost)
    assert_response :success
  end

  test "should get edit" do
    get edit_micropost_url(@micropost)
    assert_response :success
  end

  test "should update micropost" do
    patch micropost_url(@micropost), params: { micropost: { content: @micropost.content, user_id: @micropost.user_id } }
    assert_redirected_to micropost_url(@micropost)
  end

  test "should destroy micropost" do
    assert_difference('Micropost.count', -1) do
      delete micropost_url(@micropost)
    end

    assert_redirected_to microposts_url
  end
end
app\models\user.rb
class User < ApplicationRecord
    has_many :microposts
    validates :name, presence: true
    validates :email, presence: true
end
app\models\micropost.rb
class Micropost < ApplicationRecord
    belongs_to :user
    validates :content, length: { maximum: 140 }, presence: true
end

なんでや。

ソースを調べていくうちに、

app\models\user.rb
    has_many :microposts
app\models\micropost.rb
    belongs_to :user

この部分を削ると、エラーが発生しなくなった。
userテーブルとmicropostテーブルが関連していることで発生しているのか・・・?

あ、そうか。
userテーブルにデータがないから、
micropostテーブルにfixtureからデータを入れようとしても入らないのか。

setup do内に userテーブルのデータを登録する処理を追加する。

test\controllers\microposts_controller_test.rb
require 'test_helper'

class MicropostsControllerTest < ActionDispatch::IntegrationTest
  setup do
    @user = users(:one)
    post users_url, params: { user: { name: @user.name, email: @user.email } }
    @micropost = microposts(:one)
  end

  test "should get index" do
    get microposts_url
    assert_response :success
  end

  test "should get new" do
    get new_micropost_url
    assert_response :success
  end

  test "should create micropost" do
    assert_difference('Micropost.count') do
      post microposts_url, params: { micropost: { content: @micropost.content, user_id: @micropost.user_id } }
    end

    assert_redirected_to micropost_url(Micropost.last)
  end

  test "should show micropost" do
    get micropost_url(@micropost)
    assert_response :success
  end

  test "should get edit" do
    get edit_micropost_url(@micropost)
    assert_response :success
  end

  test "should update micropost" do
    patch micropost_url(@micropost), params: { micropost: { content: @micropost.content, user_id: @micropost.user_id } }
    assert_redirected_to micropost_url(@micropost)
  end

  test "should destroy micropost" do
    assert_difference('Micropost.count', -1) do
      delete micropost_url(@micropost)
    end

    assert_redirected_to microposts_url
  end
end

実行結果エラーがなくなりました。

テストOK
C:\Users\ror\sampleapp>rails t
Run options: --seed 5100

# Running:

..............

Finished in 12.616316s, 1.1097 runs/s, 1.4267 assertions/s.
14 runs, 18 assertions, 0 failures, 0 errors, 0 skips
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?