0

I've got these both files and I'd like to test it in Rails 4.2 (using RSpec 3.2) But when running I get the exception.

Does anyone has an idea to fix this?

new.html.erb_spec.rb:

require 'rails_helper'

RSpec.describe "products/new", type: :view do
  before(:each) do
    assign(:product, Product.new())
  end

  it "renders new product form" do
    render

    # assert_select "form[action=?][method=?]", products_path, "post" do
    # end
  end
end

new.html.erb:

<h1>New Product</h1>


<%= link_to 'Back', products_path %>

Exception:

  1) products/new renders new product form
     Failure/Error: render
     ActionView::Template::Error:
       undefined local variable or method `products_path' for #<#<Class:0x007fc7da6d6240>:0x007fc7da6cf3a0>
     # ./app/views/products/new.html.erb:4:in `_app_views_products_new_html_erb__2726879035604323536_70248169634620'
     # /Users/endem/.rvm/gems/ruby-2.2.1/gems/actionview-4.2.4/lib/action_view/template.rb:145:in `block in render'
     # /Users/endem/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.4/lib/active_support/notifications.rb:164:in `block in instrument'
     # /Users/endem/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.4/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
     # /Users/endem/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.4/lib/active_support/notifications.rb:164:in `instrument'

In regards to the other question: Using that solution didn't help. If using the proposed

spree.products_path

the site breaks. AND: The tests aren't running...

It's strange that these tests are automatically(!) generated by a generator and aren't working.

meme
  • 81
  • 2
  • 12
  • Is it a valid file name `new.html.erb_spec.rb`? – Pavan Oct 22 '15 at 12:00
  • Looks to me like you don't have products_path set up in your routes file. What does your routes look like for products? – bmac151 Oct 22 '15 at 12:05
  • Yes new.html.erb_spec.rb is the file name of the spec located in specs/views/. It's generated by rails generator... (Not my idea.... :D:D:D) – meme Oct 22 '15 at 13:20
  • The routes are correct. I've used resources for the product. So they should be correct. With rake routes I can see that producs_path is set correctly. – meme Oct 22 '15 at 13:20
  • Possible duplicate of [RSpec not finding my named routes](http://stackoverflow.com/questions/19101618/rspec-not-finding-my-named-routes) – Drenmi Oct 22 '15 at 14:30

2 Answers2

0

Do you have

RSpec.configure do |config|
  config.include Rails.application.routes.url_helpers
  ...
end

included in your rails_helper?

Similar question was posted here.

Community
  • 1
  • 1
bmac151
  • 505
  • 3
  • 8
  • No I haven't but after adding it it doesn't do anything. The new/old error: 'Failures: 1) products/new renders new product form Failure/Error: render ActionView::Template::Error: undefined local variable or method `products_path' for #<#:0x0000000a780eb0> # ./app/views/products/new.html.erb:4:in `_app_views_products_new_html_erb__1064281153_87403260' # ./spec/views/products/new.html.erb_spec.rb:9:in `block (2 levels) in ' ' – meme Oct 22 '15 at 13:53
0

I've found the solution... It hasn't got anything to do with imports.

Within my Gemfile I had the following line, which I found somewhere on the internet:

gem 'rspec-rails', '~> 3.0', require: 'rspec/rails'

I fixed it to

gem 'rspec-rails', '~> 3.3'

After that all the errors about missing path etc are gone on my rails app using rails 4.2.4 and rspec 3.3

Thanks for your help!!!

meme
  • 81
  • 2
  • 12