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 messageFailures:
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
Related articles
- ruby on rails - rails tutorial chapter 13 cooperation between heroku and aws s3 region
- ruby on rails 6 - rails tutorial chapter 14 about follow, unfollow, following? methods
- ruby - i got stuck in chapter 5 of the rails tutorial i want to apply it because applicationhtmlerb is not applied
- ruby - rails tutorial chapter 4 test fails
- ruby on rails - in the rails tutorial (4th edition, chapter 5), the layout of only the login page is broken
- ruby - rails tutorial chapter 2 (6th edition)
- ruby on rails 6 - rails tutorial (6th edition, chapter 8, list 833) what is the difference between logged_in? and is_logged_in ?
- cloud9 - rails tutorial chapter 12 problems that rails test does not pass
- ruby - about the delete function in rails
- windows - rails tutorial chapter 13 (1344) image upload in production environment does not work (s3, iam)
- ruby on rails - rails tutorial tests that should pass fail with an error [6th edition]
- ruby on rails 6 - rails tutorial login [remember me] checkbox test
- ruby on rails - [rails] i want to dynamically add and delete forms using cocoon
- ruby on rails 6 - the delete function cannot be implemented well
- ruby on rails 5 - the setup for everyday rails chapter 2 doesn't work (could not find shared context "project setup")
- ruby on rails - rails tutorial about git config settings
- ruby on rails - i want to write a rails tutorial test in rspec
- ruby on rails - i would like to add an automatic address input function to the new registration screen
- ruby on rails - i want to resolve rails wrong number of arguments
Related questions
- ruby - rspec that would be correct does not pass
- ruby - rspec the expected model is not created
- ruby - i get an error when testing rspec instance variables
- ruby : How to write a test that does not depend on the environment in which it is executed
- ruby - output the total integer value held by each user
- ruby - unable to deploy on capistrano
- ruby - i want to use two rails renders
- html - i can't set the regular expression well
- ruby - rails i want to make the created_at time the same when creating multiple data at once
- ruby - dynamic form implementation in nested_form
It seems to stay at 0 because the micropost wasn't created before it was deleted.
let (: micropost)
Fromlet! (: micropost)
Change to in advancemicropost
It seems to work as expected when you create.