Home>

In Chapter 13 of the Rails tutorial, Listing 13-55, I wrote an integration test for the UI in RSpec as a beginner, but I get an error in the #destroy part. I've been reviewing the code from the beginning of Chapter 13 and researching it for several hours, but it doesn't help. Please teach me.

Error message
Failures:
  1) microposts interface #destroy post delete post
     Failure/Error:
       expect do
         delete micropost_path (micropost)
       end.to change (Micropost,: count) .by (-1)
       expected `Micropost.count` to have changed by -1, but was changed by 0
     # ./spec/requests/microposts_interface_spec.rb:30: in `block (3 levels) in<top (required)>'
Finished in 3.79 seconds (files took 19.72 seconds to load)
3 examples, 1 failure
Failed examples:
rspec ./spec/requests/microposts_interface_spec.rb:27 #microposts interface #destroy post delete post
test.log
[1m [35m (0.1ms) [0m [1m [34m SELECT COUNT (*) FROM "microposts" [0m
  [1m [35m (0.1ms) [0m [1m [35mSAVEPOINT active_record_1 [0m]
  [1m [36mMicropost Create (0.2ms) [0m [1m [32mINSERT INTO "microposts" ("content", "user_id", "created_at", "updated_at") VALUES (?,?,?,?)] [0m [[[ "content", "This is test post"],

 ["user_id", 1046959424],

 ["created_at", "2020-09-26 15: 20: 13.351638"],

 ["updated_at", "2020-09-26 15: 20: 13.351638"]]
  [1m [35m (5.5ms) [0m [1m [35m RELEASE SAVEPOINT active_record_1 [0m]
Started DELETE "/ microposts/1033843181" for 127.0.0.1 at 2020-09-27 00:20:13 +0900
Processing by MicropostsController # destroy as HTML
  Parameters: {"id" =>"1033843181"}
  [1m [36m User Load (0.1ms) [0m [1m [34m SELECT "users". * FROM "users" WHERE "users". "Id" =? LIMIT? [0m [[" id", 1046959424],

 ["LIMIT", 1]]
  [1m [36mMicropost Load (0.2ms) [0m [1m [34mSELECT "microposts". * FROM "microposts" WHERE "microposts". "User_id" =? AND "microposts". "Id" =? ORDER BY "microposts". "created_at" DESC LIMIT? [0m [["user_id", 1046959424],

 ["id", 1033843181],

 ["LIMIT", 1]]
  [1m [35m (0.1ms) [0m [1m [35mSAVEPOINT active_record_1 [0m]
  [1m [36mMicropost Destroy (0.1ms) [0m [1m [31mDELETE FROM "microposts" WHERE "microposts". "Id" =? [0m [["id", 1033843181]]]
  [1m [35m (0.1ms) [0m [1m [35m RELEASE SAVEPOINT active_record_1 [0m]
Redirected to http://www.example.com/home
Completed 302 Found in 4ms (ActiveRecord: 0.5ms)
  [1m [35m (0.1ms) [0m [1m [34m SELECT COUNT (*) FROM "microposts" [0m
  [1m [35m (0.4ms) [0m [1m [31mrollback transaction [0m]
/spec/requests/microposts_interface_spec.rb
require "rails_helper"
RSpec.describe "microposts interface" do
  let (: user) {create (: user)}
  let (: micropost) {user.microposts.create (content: "This is test post")}
  describe "#create post" do
    it "submit invalid post" do
      sign_in_as (user)
      visit home_path
      expect do
        post microposts_path, params: {micropost: {content: ""}}
      end.to change (Micropost,: count) .by (0)
      expect (response) .to render_template "static_pages/home"end
    it "submit valid post" do
      sign_in_as (user)
      visit home_path
      expect do
        post microposts_path, params: {micropost: {content: "This is second post"}}
      end.to change (Micropost,: count) .by (1)
      expect (response) .to redirect_to home_path
    end
  end
  describe "#destroy post" do
    it "delete post" do
      sign_in_as (user)
      visit home_path
      expect do
        delete micropost_path (micropost)
      end.to change (Micropost,: count) .by (-1)
      expect (response) .to redirect_to home_path
    end
  end
end
microposts.controller.rb
class MicropostsController</pre>
<strong>/spec/factories/users.rb</strong>
<pre><code>RSpec.configure do | config |
  config.include FactoryBot :: Syntax :: Methods
end
FactoryBot.define dofactory: user do
    name {"Example User"}
    sequence (: email) {| n | "user_ # {n} @ example.com"}
    password {"password"}
    password_confirmation {"password"}
    activated {true}
    activated_at {Time.zone.now}
  end
end
Supplementary information (FW/tool version, etc.)
source'https://rubygems.org'
gem'rails', '5.2.4'
gem'bootstrap-sass', '3.4.1'
gem'sass-rails','>= 3.2'
gem'puma', '3.9.1'
gem'uglifier', '3.2.0'
gem'coffee-rails', '4.2.2'
gem'jquery-rails', '4.3.1'
gem'turbolinks', '5.0.1'
gem'jbuilder', '2.7.0'
gem "haml-rails", "~>2.0"
gem'bootsnap', require: false
gem'bcrypt', '3.1.12'
gem'faker', '1.7.3'
gem'will_paginate', '3.1.7'
gem'bootstrap-will_paginate', '1.0.0'
gem'carrierwave', '1.2.2'
gem'mini_magick', '4.7.0'
group: development,: test do
  gem'sqlite3', '1.3.13'
  gem'byebug', '9.0.6', platform:: mri
  gem'rspec-rails','~>3.8'
  gem'spring-commands-rspec'
  gem'capybara'
  gem'webdrivers'
  gem'factory_bot_rails'
  gem'shoulda-matchers'
  gem'rails-controller-testing'
end
group: development do
  gem'web-console', '3.5.1'
  gem'listen', '3.1.5'
  gem'spring', '2.0.2'
  gem'spring-watcher-listen', '2.0.1'
end
group: test do
  gem'minitest', '5.14.2'
  gem'minitest-reporters', '1.1.14'
  gem'guard', '2.16.2'
  gem'guard-minitest', '2.4.4'
end
group: production do
  gem'pg', '0.20.0'
  gem'fog', '1.42'
end
  • Answer # 1

    It seems to stay at 0 because the micropost wasn't created before it was deleted.

    let (: micropost)Fromlet! (: micropost)Change to in advancemicropostIt seems to work as expected when you create.

Related articles