Rails 4, Rspec dryly testing routes - ruby-on-rails

I am trying to dry up my testing and would like to check routes. app.send("home_path") returns "/home" in the console but when I run it in tests it comes back as an undefined method.
Any ideas?
1) StaticPages Check paths should have content Home
Failure/Error: visit app.send("#{term.downcase}_path")
NoMethodError:
undefined method `home_path' for #<test::Application:0x007f917a48e078>
# ./spec/requests/static_pages_spec.rb:18:in `block (4 levels) in <top (required)>'
["Home", "About"].each do |term|
describe "Check paths" do
before(:each) do
visit app.send("#{term.downcase}_path")
end
it "should have content #{term}" do
expect(page).to have_content(term)
end
end
end

Related

I keep getting this error " NoMethodError:undefined method `visit' for #<RSpec::ExampleGroups::" in rspec and rails 4.1

I'm working on this http://net.tutsplus.com/tutorials/ruby/the-intro-to-rails-screencast-i-wish-i-had/ and I keep getting this error.
I'm using rails 4.1
terminal
Failures:
1) Tasks GET /tasks display some task
Failure/Error: visit tasks_path
NoMethodError:
undefined method `visit' for #<RSpec::ExampleGroups::Tasks::GETTasks:0x007fcfbd633758>
# /Users/estebangallego/.rvm/gems/ruby-2.2.0/gems/actionpack-4.1.8/lib/action_dispatch/testing/assertions/routing.rb:171:in `method_missing'
# /Users/estebangallego/.rvm/gems/ruby-2.2.0/gems/actionpack-4.1.8/lib/action_dispatch/testing/integration.rb:396:in `method_missing'
# ./spec/features/task_spec.rb:7:in `block (3 levels) in <top (required)>'
2) Tasks GET /tasks creates a new task
Failure/Error: visit "/"
NoMethodError:
undefined method `visit' for #<RSpec::ExampleGroups::Tasks::GETTasks:0x007fcfbe572ea8>
# /Users/estebangallego/.rvm/gems/ruby-2.2.0/gems/actionpack-4.1.8/lib/action_dispatch/testing/assertions/routing.rb:171:in `method_missing'
# /Users/estebangallego/.rvm/gems/ruby-2.2.0/gems/actionpack-4.1.8/lib/action_dispatch/testing/integration.rb:396:in `method_missing'
# ./spec/features/task_spec.rb:13:in `block (3 levels) in <top (required)>'
Finished in 0.01248 seconds (files took 1.69 seconds to load)
4 examples, 2 failures, 2 pending
Failed examples:
rspec ./spec/features/task_spec.rb:5 # Tasks GET /tasks display some task
rspec ./spec/features/task_spec.rb:12 # Tasks GET /tasks creates a new task
I tried "features/task_spect.rb" and "requests/tasks_spect.rb"
require "rails_helper"
RSpec.describe "Tasks", type: :request do
describe "GET /tasks" do
it "display some task" do
#task = Task.create :task => "go to bed"
visit tasks_path
page.should have_content "go to bed"
end
it "creates a new task" do
visit "/"
fill_in "Task", :with => "go to work"
click_button "Create Task"
current_path.should == root_path
page.should have_content "go to work"
save_and_open_page
end
end
end
gems
group :development, :test do
gem 'rspec-rails'
gem 'capybara'
end
new rspec.rb
require 'rails_helper'
RSpec.describe "Tasks", type: :request do
config.include Capybara::DSL
describe "GET /tasks" do
it "display some task" do
#task = Task.create :task => 'go to bed'
visit root_path
page.should have_content 'go to bed'
end
it "creates a new task" do
visit '/'
fill_in 'Task', :with => 'go to work'
click_button 'Create Task'
current_path.should == task_path
page.should have_content 'go to work'
save_and_open_page
end
end
end
I have fixed the error and post it in my github app
https://github.com/rajcybage/tasks
please see the commit
https://github.com/rajcybage/tasks/commit/fa6fa54e60717f805e040f0eb767dc0e3ef4d434
run bundle exec rake spec:features
rajarshi#rajarshi-X200CA:~/tasks$ bundle exec rake spec:features
/home/rajarshi/.rbenv/versions/2.1.4/bin/ruby -I/home/rajarshi/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.3/lib:/home/rajarshi/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/rspec-support-3.2.2/lib /home/rajarshi/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/rspec-core-3.2.3/exe/rspec --pattern ./spec/features/\*\*/\*_spec.rb
Randomized with seed 14426
..
Deprecation Warnings:
Requiring `rspec/autorun` when running RSpec via the `rspec` command is deprecated. Called from /home/rajarshi/.rbenv/versions/2.1.4/lib/ruby/gems/2.1.0/gems/activesupport-4.1.8/lib/active_support/dependencies.rb:247:in `require'.
Using `should` from rspec-expectations' old `:should` syntax without explicitly enabling the syntax is deprecated. Use the new `:expect` syntax or explicitly enable `:should` with `config.expect_with(:rspec) { |c| c.syntax = :should }` instead. Called from /home/rajarshi/tasks/spec/features/task_spec.rb:18:in `block (3 levels) in <top (required)>'.
If you need more of the backtrace for any of these deprecations to
identify where to make the necessary changes, you can configure
`config.raise_errors_for_deprecations!`, and it will turn the
deprecation warnings into errors, giving you the full backtrace.
2 deprecation warnings total
Finished in 2.06 seconds (files took 6.03 seconds to load)
2 examples, 0 failures
Randomized with seed 14426
rajarshi#rajarshi-X200CA:~/tasks$

Capybara: undefined method 'visit' - Test is already in spec/features

I have just started with RSpec and Capybara but got stuck on my first test.
Here's me test code located in spec/features/pages_spec.rb :
require 'rails_helper'
RSpec.describe "Pages", :type => :request do
describe "About Page" do
it "should have the content 'About Us'" do
visit '/pages/about'
page.should have_content('About Us')
end
end
end
Running the test i get the following error :
01:06:59 - INFO - Running: spec
F
Failures:
1) Pages About Page should have the content 'About Us'
Failure/Error: visit '/pages/about'
NoMethodError:
undefined method 'visit' for #<RSpec::ExampleGroups::Pages::AboutPage:0x007f975afe7380>
# ./spec/features/pages_spec.rb:6:in `block (3 levels) in <top (required)>'
# ./spec/rails_helper.rb:44:in `block (3 levels) in <top (required)>'
# ./spec/rails_helper.rb:43:in `block (2 levels) in <top (required)>'
Finished in 0.02569 seconds (files took 1.65 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/features/pages_spec.rb:5 # Pages About Page should have the content 'About Us'
I've been searching about this for about an hour and everywhere I find the solution of moving the test code from spec/requests to spec/features.
I've also seen this here : http://www.rubydoc.info/gems/rspec-rails/file/Capybara.md which most of the solution suggests and it is not recommended to use.
# not recommended!
RSpec.configure do |c|
c.include Capybara::DSL, :file_path => "spec/requests"
end
I've no idea how to proceed. Your help will be appreciated.
Either take :type => :request out of the describe block (which overrides the spec type determined from the directory location) or change it to :type => :feature.

undefined method `visit` rspec/capybara

I am following Michael Hartls tutorial on rspec and I am getting this error
bundle exec rspec spec/requests/static_pages_spec.rb
F
Failures:
1) Static pages Home page should have the content 'Sample App'
Failure/Error: visit '/static_pages/home'
NoMethodError:
undefined method `visit' for #<RSpec::ExampleGroups::StaticPages::HomePage:0x007fdddbdf6a90>
# ./spec/requests/static_pages_spec.rb:6:in `block (3 levels) in <top (required)>'
Finished in 0.00052 seconds (files took 0.1562 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/requests/static_pages_spec.rb:5 # Static pages Home page should have the content 'Sample App'
for the rspec test.
require 'spec_helper'
describe "Static pages" do
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
page.should have_content('Sample App')
end
end
end
I don't believe that the reason I am getting this error is the test case is failing but rather this:
NoMethodError:
undefined method `visit' for #<RSpec:....
I am not sure why there is an undefined method visit
maybe you just missed:
config.include Capybara::DSL
also you could look to the same qestion here

Rspec - nameError: undefined local variable or method `sammiches_path'

I am a bit confused here. I am trying to do TDD and have run into an issue. Rspec is telling me the following-
1) Sammiches GET /sammiches display some sammiches
Failure/Error: visit sammiches_path
NameError:
undefined local variable or method `sammiches_path' for #<RSpec::Core::ExampleGroup::Nested_4::Nested_1:0x007fa7afcfed70>
# ./spec/requests/sammiches_spec.rb:7:in `block (3 levels) in <top (required)>'
2) Sammiches GET /sammiches creates a new sammich
Failure/Error: visit sammiches_path
NameError:
undefined local variable or method `sammiches_path' for #<RSpec::Core::ExampleGroup::Nested_4::Nested_1:0x007fa7ae11e6f0>
# ./spec/requests/sammiches_spec.rb:12:in `block (3 levels) in <top (required)>'
This is what my spec file looks like
require 'spec_helper'
describe "Sammiches" do
describe "GET /sammiches" do
it "display some sammiches" do
#sammich = Sammich.create :name => 'bacon'
visit sammiches_path
page.should have_content 'bacon'
end
it "creates a new sammich" do
visit sammiches_path
fill_in 'Sammich', :with => 'lechuga'
click_button 'add Sammich'
current_path.should = root_path
page.should have_content 'lechuga'
save_and_open_page
end
end
end
My routes are -
sammiches_index GET /sammiches/index(.:format) sammiches#index
Sammiches GET /Sammiches(.:format) Sammiches#index
POST /Sammiches(.:format) Sammiches#create
new_Sammich GET /Sammiches/new(.:format) Sammiches#new
edit_Sammich GET /Sammiches/:id/edit(.:format) Sammiches#edit
Sammich GET /Sammiches/:id(.:format) Sammiches#show
PUT /Sammiches/:id(.:format) Sammiches#update
DELETE /Sammiches/:id(.:format) Sammiches#destroy
root / Sammiches#index
Routes-
Sammiches::Application.routes.draw do
get "Sammiches/index"
resources :sammiches
root :to => 'Sammiches#index'
New Error-
1) Sammiches GET /sammiches creates a new sammich
Failure/Error: visit sammiches_path
ActionView::Template::Error:
undefined method `sammich' for #<Sammich:0x007fd0007d2f80>
# ./app/views/sammiches/index.html.erb:4:in `block in _app_views_sammiches_index_html_erb___2703584807867277870_70265660050820'
# ./app/views/sammiches/index.html.erb:2:in `_app_views_sammiches_index_html_erb___2703584807867277870_70265660050820'
# ./spec/requests/sammiches_spec.rb:12:in `block (3 levels) in <top (required)>'
2) Sammiches GET /sammiches display some sammiches
Failure/Error: visit sammiches_path
ActionView::Template::Error:
undefined method `sammich' for #<Sammich:0x007fd00006f4f0>
# ./app/views/sammiches/index.html.erb:4:in `block in _app_views_sammiches_index_html_erb___2703584807867277870_70265660050820'
# ./app/views/sammiches/index.html.erb:2:in `_app_views_sammiches_index_html_erb___2703584807867277870_70265660050820'
# ./spec/requests/sammiches_spec.rb:7:in `block (3 levels) in <top (required)>'
I am at a bit of a loss here. Any advice would be greatly appreciated.
Thanks.
You seem to have added resources :Sammiches in config/routes.rb.
Change it to resources :sammiches. Sticking to the convention, helps. :)
Your route file should look like:
Sammiches::Application.routes.draw do
resources :sammiches
root :to => 'sammiches#index'

Undefined method 'action' running single RSpec file for a rails controller

I have a peculiar situation - an rspec file fails when run independently, but run okay when run as part of the entire suite.
Failure/Error: visit oauth_callback_path
NoMethodError:
undefined method `action' for MyController:Class
# <internal:prelude>:10:in `synchronize'
# ./spec/requests/login_spec.rb:xx:in `block (5 levels) in <top (required)>'
# ./spec/requests/login_spec.rb:xx:in `block (4 levels) in <top (required)>'
Simplified spec:
require 'spec_helper'
class MyController
def oauth_response
sign_in(
ENV['TEST_ACCESS_TOKEN'],
ENV['TEST_ACCESS_SECRET'])
redirect_to root_path
end
end
describe 'logging in' do
it 'login' do
visit oauth_callback_path
response.should be_success
end
end
I believe the problem is that MyController is not extending ApplicationController. That's why the method action is not defined for MyController.
The class MyController appears to be blocking Rails magic class loading. Either the test should explicitly require the controller, or the extension should be defined with MyController.class_eval

Resources