当我 运行 Rspec 功能时路由增量
Routes Increment when I Run Rspec feature
因此,由于某些原因,当我 运行 我的评论功能测试时,我的路线增加了 11。
图表 A:我在 comment_spec.rb
上点击保存
RSpec.feature "Adding comments to movies" do
before do
@kyle = Admin.create(email: "kyle@example.com", password: "password")
@jill = Member.create(email: "jill@example.com", password: "password")
@movie = Movie.create!(title: "First movie", synopsis: "Synopsis of first movie", year_released: '2000', admin: @kyle)
end
scenario "permits a signed in member to write a comment" do
login_as(@jill, scope: :member)
visit "/"
click_link @movie.title
fill_in "New Review", with: "An awesome movie"
click_button "Add Review"
expect(page).to have_content("Review has been created")
expect(page).to have_content("An awesome movie")
# must implement a nested route in order for this to work.
expect(current_path).to eq(movie_path(@movie.comments.last.id))
end
end
然后测试通过
moviesmoviesmovies/spec/features/comments_spec.rb:3)
Finished in 0.99879 seconds (files took 2.4 seconds to load) 1
example, 0 failures
现在,我在终端中按下回车键,得到了这个:
Adding comments to movies permits a signed in member to write a comment
Failure/Error: expect(current_path).to eq(movie_path(@movie.comments.last.id))
expected: "/movies/2"
got: "/movies/4"
(compared using ==)
# ./spec/features/comments_spec.rb:26:in `block (2 levels) in <top (required)>'
然后我再次保存 comment_spec.rb 并得到这个:
Adding comments to movies permits a signed in member to write a comment
Failure/Error: expect(current_path).to eq(movie_path(@movie.comments.last.id))
expected: "/movies/3"
got: "/movies/15"
(compared using ==)
# ./spec/features/comments_spec.rb:26:in `block (2 levels) in <top (required)>'
发生这种情况后,我运行:
bundle exec rails db:test:prepare
然后测试再次通过,但上面重复。到底是什么让这一切发生?哈哈,但真的吗?
My GitHub 如果需要。
您遇到的问题是您的数据库在每次功能测试后都没有像您预期的那样被重置。具体来说,问题出在 Capybara 和您的规范配置中设置的 config.use_transactional_fixtures = true
行。
Capybara 使用虚拟浏览器将您的应用程序作为外部进程进行有效测试。这意味着您的水豚测试只能看到您应用程序的客户端行为(即 page
变量)。换句话说,从 Capybara 的角度来看,您的应用是 "Black box"。因此,capaybara 看不到像您的 session
、params
或 request
变量这样的控制器对象,也看不到或控制在测试期间发生的特定数据库事务。
与其依赖事务固定装置,不如考虑使用数据库清理器,它会在每次功能测试后手动重置数据库。您可以在此处找到 gem:https://github.com/DatabaseCleaner/database_cleaner. Make sure you follow the directions on integrating with Capybara: https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example。
因此,由于某些原因,当我 运行 我的评论功能测试时,我的路线增加了 11。
图表 A:我在 comment_spec.rb
上点击保存RSpec.feature "Adding comments to movies" do
before do
@kyle = Admin.create(email: "kyle@example.com", password: "password")
@jill = Member.create(email: "jill@example.com", password: "password")
@movie = Movie.create!(title: "First movie", synopsis: "Synopsis of first movie", year_released: '2000', admin: @kyle)
end
scenario "permits a signed in member to write a comment" do
login_as(@jill, scope: :member)
visit "/"
click_link @movie.title
fill_in "New Review", with: "An awesome movie"
click_button "Add Review"
expect(page).to have_content("Review has been created")
expect(page).to have_content("An awesome movie")
# must implement a nested route in order for this to work.
expect(current_path).to eq(movie_path(@movie.comments.last.id))
end
end
然后测试通过
moviesmoviesmovies/spec/features/comments_spec.rb:3)
Finished in 0.99879 seconds (files took 2.4 seconds to load) 1 example, 0 failures
现在,我在终端中按下回车键,得到了这个:
Adding comments to movies permits a signed in member to write a comment
Failure/Error: expect(current_path).to eq(movie_path(@movie.comments.last.id))
expected: "/movies/2"
got: "/movies/4"
(compared using ==)
# ./spec/features/comments_spec.rb:26:in `block (2 levels) in <top (required)>'
然后我再次保存 comment_spec.rb 并得到这个:
Adding comments to movies permits a signed in member to write a comment
Failure/Error: expect(current_path).to eq(movie_path(@movie.comments.last.id))
expected: "/movies/3"
got: "/movies/15"
(compared using ==)
# ./spec/features/comments_spec.rb:26:in `block (2 levels) in <top (required)>'
发生这种情况后,我运行:
bundle exec rails db:test:prepare
然后测试再次通过,但上面重复。到底是什么让这一切发生?哈哈,但真的吗?
My GitHub 如果需要。
您遇到的问题是您的数据库在每次功能测试后都没有像您预期的那样被重置。具体来说,问题出在 Capybara 和您的规范配置中设置的 config.use_transactional_fixtures = true
行。
Capybara 使用虚拟浏览器将您的应用程序作为外部进程进行有效测试。这意味着您的水豚测试只能看到您应用程序的客户端行为(即 page
变量)。换句话说,从 Capybara 的角度来看,您的应用是 "Black box"。因此,capaybara 看不到像您的 session
、params
或 request
变量这样的控制器对象,也看不到或控制在测试期间发生的特定数据库事务。
与其依赖事务固定装置,不如考虑使用数据库清理器,它会在每次功能测试后手动重置数据库。您可以在此处找到 gem:https://github.com/DatabaseCleaner/database_cleaner. Make sure you follow the directions on integrating with Capybara: https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example。