dimanche 8 septembre 2019

Tests failed after add 'belongs_to' to model in Rails application

Tests failed after add belongs_to in Rails

I have 2 models in Rails application:

class Micropost < ApplicationRecord
  belongs_to :user # Test failed after add this string
  validates :content, length: { maximum: 140 }, presence: true
end

class User < ApplicationRecord
  has_many :microposts

  validates :name, presence: true
  validates :email, presence: true
end

I added string "belongs_to :user" to model "Micropost". After that I ran tests, and they failed:

rails test

  1) Failure:
MicropostsControllerTest#test_should_create_micropost [/home/kiselev/project/toy_app/test/controllers/microposts_controller_test.rb:19]:
"Micropost.count" didn't change by 1.
Expected: 3
  Actual: 2

  2) Failure:
MicropostsControllerTest#test_should_update_micropost [/home/kiselev/project/toy_app/test/controllers/microposts_controller_test.rb:38]:
Expected response to be a <3XX: redirect>, but was a <200: OK>

I have these 2 tests:

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 update micropost" do
  patch micropost_url(@micropost), params: { micropost: { content: @micropost.content, user_id: @micropost.user_id } }
  assert_redirected_to micropost_url(@micropost)
end

How can I improve these tests to pass?

Aucun commentaire:

Enregistrer un commentaire