【发布时间】:2018-11-26 12:16:03
【问题描述】:
我正在关注restaurantly Rails 教程,同时每当我遇到 Rails 版本 4 和我正在使用的 Rails 版本 5 之间的版本(主要是语法)问题时,我都会进行自己的更改。
在完成教程中的this 页面的步骤后,我认为在我的destroy 函数测试失败后,这是另一个语法差异。
rspec spec/features/restaurants_spec.rb
Capybara starting Puma...
* Version 3.12.0 , codename: Llamas in Pajamas
* Min threads: 0, max threads: 4
* Listening on tcp://127.0.0.1:41471
.F
Failures:
1) destroy links work displays Restaurantly Spots!
Failure/Error: expect(page).to have_no_content 'mc ruby'
expected not to find text "mc ruby" in "mc ruby"
# ./spec/features/restaurants_spec.rb:27:in `block (3 levels) in <top (required)>'
Finished in 7.25 seconds (files took 2.24 seconds to load)
2 examples, 1 failure
Failed examples:
rspec ./spec/features/restaurants_spec.rb:20 # destroy links work displays Restaurantly Spots!
我运行puma -e 'test' 以在“测试”环境中手动运行应用程序,并在浏览器中自己逐步执行控制器操作。我发现我有同样的失败行为。我定义的所有操作,包括create、edit、update 和show,都按预期工作。但是destroy 的行为类似于show,只是显示该项目的显示页面,或者重定向到根目录并删除该项目。
如果我在包括destroy 函数的“开发”环境中运行rails server,这一切都可以正常工作。
以下是我的应用程序的一些不同部分,以防万一:
app/controllers/restaurants_controller.rb:
def destroy
@restaurant = Restaurant.find_by_id params[:id]
@restaurant.destroy
redirect_to root_path
end
app/views/restaurants/index.html.haml
.row
.large-8.columns.large-centered
%h3.subheader.center
Restaurantly Spots!
.row
.large-8-columns.large-centered
- @restaurants.each do |restaurant|
%h5.subheader
= restaurant.name
= link_to "edit", edit_restaurant_path(restaurant)
= link_to "destroy", restaurant_path(restaurant), method: :destroy
我的 Gemfile 的一部分,包括开发、测试 gem:
group :test, :development do
gem 'rspec-rails'
gem 'factory_bot_rails'
gem 'capybara'
gem 'pry'
gem 'pry-byebug'
end
我曾一度删除了测试数据库,然后又重新创建了它。我执行此操作的方式与我最初与开发数据库一起创建它的方式相同,先运行bundle exec rake db:create,然后运行bundle exec rake db:migrate,最后运行bundle exec rake db:test:prepare。
config/database.yml
development:
adapter: postgresql
encoding: unicode
database: restaurantly_dev
pool: 5
host: ""
timeout: 5432
test:
adapter: postgresql
encoding: unicode
database: restaurantly_test
pool: 5
host: ""
timeout: 5432
任何想法为什么它在开发和测试环境之间的破坏功能表现不同?
【问题讨论】:
-
测试是什么样的?此外,
method应该是:delete而不是:destroy(它是 REST 请求 METHOD(GET POST DELETE PATCH 等)的名称,而不是方法/操作名称。此外,Rails 使用 javascript 将链接与 @987654343 @ 在 DELETE 请求中,因此如果您的测试驱动程序不支持 javascript,则删除链接将不起作用。
标签: ruby-on-rails rspec-rails puma