ruby - How to do a feature test in Rails with Capybara and how to know 500 error? -
i created route in rails application as:
scope 'product' '/', to: 'product#sort', as: 'product' end
then want test feature capybara as:
# spec/features/product_spec.rb require 'spec_helper' feature 'product', type: :feature describe 'main page' context 'before login' 'show product list' visit product_path expect(page).to have_content('product list') end end end end
but after run rspec in terminal, showed me:
failure/error: expect(page).to have_content('product list') expected find text "product list" in "500 internal server error"
why gave me 500 error? happened? how can see error detail?
in controller , view, set @product_categories
:
in controller:
# app/controllers/product_controller.rb class productcontroller < applicationcontroller def sort @product_categories = productcategory.all @products = product.all end end
in view:
# app/views/product/sort.html.erb <h1>product list</h1> <% @product_categories.each |category| %> <%= category.name %> <% end %> <% @products.each |product| %> <%= product.name %> <% end %>
is necessary define value in spec test source?
you're calling .each
on class. think mean
@product_categories = productcategories.all
in controller. also, convention, controllers , view folder name should pluralized, products_controller.rb
class name productscontroller
, views folder app/views/products/sort.html.erb
. in route, pluralize controller 'products#sort'
.
as test, seems using mix of rspec , capybara dsl. if use feature, don't need :type => :feature
. need combine description
, context
, , it
scenario
. refer here example in using capybara rspec built in dsl section: https://github.com/jnicklas/capybara