Setting up a new RoR project using Rspec and Capybara - ruby-on-rails

I'm trying to learn RoR following this tutorial and I'm currently in chapter 3. The tutorial works fine if I follow it line-by-line. However, the commands used in the tutorial suppress generation of default tests. When I try to keep them and possibly use them in my project, I always hit a wall somewhere.
Could you please tell me what I'm doing wrong?
$ rails new myproject
$ cd myproject/
$ echo "gem 'rspec'" >> Gemfile
$ echo "gem 'rspec-rails'" >> Gemfile
$ echo "gem 'capybara'" >> Gemfile
$ bundle install
$ bundle --binstubs
$ rails generate rspec:install
$ rails generate controller StaticPages home help about
Then I edit the spec/views/static_pages/home.html.erb_spec.rb file, to test whether capybara works:
require 'spec_helper'
#require 'capybara'
#require 'capybara/rails'
#require 'capybara/rspec'
describe "static_pages/home.html.erb" do
it 'should have a right title' do
visit '/static_pages/home'
page.should have_selector('title', :text => 'Home')
end
end
Running bin/rspec at this point, obviously, ends up with a failure. Well, a failure could have been expected. The reason for one of these failures is more alarming, though:
1) static_pages/home.html.erb should have a right title
Failure/Error: visit '/static_pages/home'
NoMethodError:
undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_5:0x00000003dfd268>
# ./spec/views/static_pages/home.html.erb_spec.rb:7:in `block (2 levels) in <top (required)>'
The visit method, which AFAIK is part of Capybara, has not been found. Uncommenting the three extra requires in home.html.erb_spec.rb does not change anything in the result.
Any ideas what I'm doing wrong? Or what I should do better?
Rails version: 3.2.6

Put your test in requests directory instead of views.

Related

RailsApp help Rspec Tutorial undefined method

Hi I am working on Daniel Kehoe's Rspec Tutorial. Everything went fine until I created the file spec/features/visitor/home_page_spec.rb . When I put the following test code in it
# Feature: Home Page
# As a visitor
# I want to visit a home page
# So I can learn more about the website
feature 'Home Page' do
# Scenario: Visit the Home Page
# Given I am a visitor
# When I visit the home page
# Then I should see "Welcome"
scenario 'Visit the Home Page' do
visit root_path
expect(page).to have_content 'Welcome'
end
end
and run rspec spec/features/visitor/home_page_spec.rb from the terminal, I got the error below. I get that the method was not defined but Daniel's tutorial just tells you to put the code in the folder above. Guess I am missing something. Thank you
rails-bootstrap/spec/features/visitor/home_page_spec.rb:7:in `<top (required)>':
undefined method `feature' for main:Object (NoMethodError)
What's in your .rspec file in the project directory? You should have:
--color
--format documentation
--require spec_helper
--require rails_helper
If the .rspec file is set up properly, you will not need additional require statements in your spec files.
The only way I get rake test to run is to add the below require to test/integration/home_page_test.rb
require 'minitest/rails/capybara'
This line is already in test/test_helper.rb so I'm not sure why it isn't picking it up. I also tried adding the .rspec file with the options suggested by Daniel. If nothing else works, try that.

Rspec/Capybara "feature" method undefined when Guard runs specs in watched files, works when run manually

I am getting a strange issue when using Guard to run my specs.
I am running a feature spec that uses the Capybara "feature" / "scenario" syntax. I am also using Spring.
Everything works fine if I run rspec spec or spring rspec in the console or rspec in the Guard shell. But, when the watched specs get run automatically by Guard, I get the following error:
/spec/features/navigation_spec.rb:1:in <top (required)>': undefined methodfeature' for main:Object (NoMethodError)
Why is it not picking up the Capybara syntax only in this specific context?
Here is the relevant code:
GuardFile
guard :rspec, :spring => true do
watch(%r{^spec/.+_spec\.rb$})
end
spec/features/navigation_spec.rb
feature "navigation" do
context "When on the home page" do
before { visit "/" }
scenario "I should see the navigation header" do
expect(page).to have_selector("div.navigation")
end
end
end
spec/spec_helper.rb
require 'capybara/rspec'
For anyone who may run into a similar issue in the future, I forgot to include require 'spec_helper' in the feature spec (like an idiot).
In Rails 4, make sure that you have included 'rails_helper' instead of 'spec_helper' on top of your specfile:
require 'rails_helper'
feature "Some Feature", :type => :feature do
..
end
And also make sure that config.disable_monkey_patching! is commented out or removed. Otherwise you will encounter problems when running your feature specs.
require 'capybara/rspec'
RSpec.configure do |config|
..
# config.disable_monkey_patching!
..
end
If you have created a .rspec file inside your project dir, also make sure to to change spec_helper to rails_helper there as well.
How are you invoking guard? It sounds like you might need to do bundle exec guard to kick things off. It could also be running under the wrong environment (unlikely, but worth a look).

Running ruby debug in rspec?

I'm trying to get Ruby debugger running in one of my specs:
describe User do
it "should be valid" do
debugger
User.new.should be_valid
end
end
When I run rspec though, I get:
debugger statement ignored, use -d or --debug option to enable debugging
I've tried the following:
rake spec --debug
rake spec --debug --trace
rake spec:models --debug
bundle exec rspec --debug
bundle exec rspec --debug spec/models/
bundle exec rspec --d spec/models/
bundle exec "rspec --debug" spec/models/
bundle exec rspec --debugger spec/models/
bundle exec --debugger rspec spec/models/
bundle --debugger exec rspec spec/models/
bundle --debugger exec rspec spec/models/
bundle exec --debugger rspec spec/models/
bundle exec rspec --debugger spec/models/
Any ideas on how to exec rspec in the right way? I'm on Rails 3.0.5, Ruby 1.9.2, RSpec 2.5.1, ruby-debug19.
Thanks,
Justin.
You will get what you want by including require 'ruby-debug' at the top of your spec:
# spec/models/user_spec.rb
require 'spec_helper'
require 'ruby-debug'
describe User do
it "should be valid" do
debugger
User.new.should be_valid
end
end
You would then run rake spec or rspec as normal
NOTE: I now prefer Ruby 2.0+ and pry. It is pretty much the same process:
require 'spec_helper'
require 'pry-debugger'
describe User do
it "should be valid" do
binding.pry
expect(User.new).to be_valid
end
end
Also, I generally put requires like this in my spec_helper file, so that pry-debugger is available to all of my specs.
You can create an .rspec configuration file in the root of your project and include the line:
--debug
For Ruby >= 1.9.2
You should install the debugger gem instead of ruby-debug19. It you use bundler, you just put this in your Gemfile:
group :test do
gem "debugger"
end
After that you can just put
rspec < 3.0
--debug
rspec >= 3.0
-rdebugger
in your .rspec file
Then you can just run
bundle exec rake spec
without any additional arguments. There is no need to modify your source code either (not even your test source code)
For ruby 2.0 I use byebug: https://github.com/deivid-rodriguez/byebug
gem 'byebug'
Code:
# spec/models/user_spec.rb
require 'spec_helper'
require 'byebug'
describe User do
it "should be valid" do
byebug
User.new.should be_valid
end
end
The best way I have found to debug in rSpec is by adding the following to your 'spec_helper.rb' file
def logger
Rails.logger
end
You can then access all the logger methods within your rSpec files and incorporate such things as tagged logging. This of course is for Rails 3 and up. If you have anything prior to Rails 3 then add this instead:
def logger
RAILS_DEFAULT_LOGGER
end
Once you have your logging statements in place you can enter
tail -f log/test.log
in your terminal shell in order to watch your logging statements while the tests are run.
Of course in your actual rspec test you would enter something such as
logger.debug "#{1.class}" # => Fixnum
If you want to filter your debug statements from the rest of your test log simply prepend a random string on to your debug statement and pipe the output of the tail command to grep.
Example:
logger.debug "random_string #{1.class}" # => Fixnum
tail -f log/test.log | grep random_string
Update
I've changed my opinion on this. You should install pry, pry-doc, and pry-debug, pry-debugger, and pry-rails. Then use binding.pry in your code to open an interactive debugger console that rules the world!
The best and cleanest option is to use --require in your .rspec file. What you put depends on which gem you use for debugging.
--color
--require pry
--require rails_helper
These correspond to command line options (-d or --debug is now deprecated).
Feel free to use debugger, ruby-debug or pry (pry-rails in your Gemfile).
For your Gemfile:
group :test, :development do
gem 'pry-rails'
end
Putting require 'ruby-debug' etc. at the top of your spec is simply more tightly coupled -- especially since here the top voted comment suggests putting it individually in ALL your files. With the new .rspec file you shouldn't need to put require 'spec_helper' or require 'rails_helper' at the top of your files anymore.
They make more sense as implicit command line arguments.

Rails - Getting RSpec Working

I'm learning how to do integration testin on my rails app. I started with cucumber but then learned since I'm just testing a models method that I should be using RSPEC. So now I'm trying to get RSPEC installed.
When I run autotest, it is only looking at the /features directory not the /spec directory.
Here's what I've done so far:
.autotest
require 'autotest/growl'
require 'autotest/fsevent'
Autotest.add_hook(:initialize) {|at|
at.add_exception %r{^\.git} # ignore Version Control System
at.add_exception %r{^./tmp} # ignore temp files, lest autotest will run again, and again...
# at.clear_mappings # take out the default (test/test*rb)
at.add_mapping(%r{^lib/.*\.rb$}) {|f, _|
Dir['spec/**/*.rb']
}
nil
}
/spec - created directory
/spec/spec_helper.rb - created
/spec/lib/mailingjob_spec.rb - created, tried to write a case that would fail as follows:
require 'spec_helper'
describe User do
before(:each) do
#valid_attributes = {
:login => "akm",
:email => "akm2000#gmail.com",
:password => "D1ff1cultPa55w0rd",
:password_confirmation => "D1ff1cultPa55w0rd"
}
end
it "should create a new instance given valid attributes" do
User.create!(#valid_attributesXXXX)
end
end
But when I run autotest this rpsec is never run only the features dir with cucumber is running. Ideas? thanks
Try:
rspec --configure autotest
From the autotest README:
[RSpec] if you want to use require
'spec_helper' -> rspec --configure
autotest

Setting up Shoulda under Test/Unit in Rails 3 (3.0.3)

I have posted this in other places but no response. Trying to get Shoulda working inside Test/Unit in Rails 3.0.3 (1.9.2). When I try to run the test (copied below), I get this error:
test/unit/practice_member_test.rb:4:in <class:PracticeMemberTest>': undefined methodcontext' for PracticeMemberTest:Class (NoMethodError)
Note that I have another Rails 3 project with Rspec including Shoulda also and it works fine via Rspec. In the failing project I tried placing "require 'shoulda'" in test helper to no avail, but when I run the debugger and type Shoulda, the object is found, so the library is being loaded.
Here is my test:
require 'test_helper'
class PracticeMemberTest < Test::Unit::TestCase
context "practice member" do
should "get global practice member count not including Demo Practice" do
assert_equal PracticeMember.practice_members_global_count, 0
practice = Factory.create(:practice_one)
practice_member = Factory.create(:practice_member)
practice_member.practice_id = practice.id
practice_member.save
practice_member = Factory.create(:practice_member)
practice_member.practice_id = practice.id
practice_member.save
assert_equal PracticeMember.practice_members_global_count, 2
end
end
end
Must be something I am overlooking as I have not seen anyone with this same issue.
Did you try adding the following to your config/environment.rb file:
Rails::Initializer.run do |config|
config.gem "shoulda", :lib => "shoulda"
end
Then
$ rake gems:install
$ rake gems:unpack
As suggested in the documentation?

Resources