Using Rails 3.2.2, I just installed rspec and tried my first test:
require 'spec_helper'
describe "Mains" do
describe "GET index" do
it "gets the page which has the proper content" do
get 'index.html'
response.status.should be(200)
response.body.should include("one")
end
end
end
When I run the spec I get
Failures:
1) Mains GET index gets the page which has the proper content
Failure/Error: response.body.should include("one")
NoMethodError:
undefined method `chomp' for ["one"]:Array
This comes strait out of Ryan Bates' Railscast 257, so I'm a bit lost
thanks in advance,
[EDIT] The issue is corrected in rspec-expectations, on master branch. You can fix it by using this in your Gemfile:
gem 'rspec-rails', '~> 2.9'
gem 'rspec-expectations', :git => "https://github.com/rspec/rspec-expectations.git", :branch => 'master'
I encountered the same issue on Rails 3.0.12 with rspec 2.9.0. Reverting to rspec 2.8.0 / rspec-rails 2.8.1 did the trick.
I raised an issue on Github, rspec/rspec-expectations, we'll see if it's really a bug.
For reference, this is the step definition:
Then /^(?:I|they) should see "([^"]*?)" in the email body$/ do |text|
current_email.default_part_body.to_s.should include(text)
end
... the step in the feature:
step %{I should see "changer de mot de passe" in the email body}
... which was raising:
undefined method `chomp' for ["changer de mot de passe"]:Array (NoMethodError)
./features/step_definitions/email_steps.rb:110:in `/^(?:I|they) should see "([^"]*?)" in the email body$/'
Related
Just trying to get through the last bit of chapter 5 in Michael Hartl's Ruby on Rails Tutorial and getting another error running RSpec tests.
The output is :
Static pages should have the right links on the layout
Failure/Error: expect(page).to have_title('About Us')
NoMethodError:
undefined method has_title?' for #<Capybara::Session>
# ./spec/requests/static_pages_spec.rb:59:inblock (2 levels) in '
and results from the line starting expect(page) in the following code in static_pages_spec.rb :
it "should have the right links on the layout" do
visit root_path
click_link "About"
expect(page).to have_title(full_title('About Us'))
end
Note : This happens running with or without Spork
Can anyone point me in the right direction please ?
Thanks,
Bazza
The have_title function is supported from Capybara 2.1. I suppose you have an older version of Capybara in your Gemfile. So, update your Gemfile with
gem 'capybara', '2.1.0'
then update Capybara like this
bundle update capybara
and rerun the specs using rspec. It should work now
Refer to this post for other options
I am getting the following error in my rspec code
undefined local variable or method `render'
here is my code:
require 'spec_helper'
describe "messages/show.html.erb" do
it "displays the text attribute of the message" do
render
rendered.should contain("Hello world!")
end
end
this is from the book : The RSpec book
below is what i added to Gemfile.
group :development , :test do
gem "rspec-rails", ">= 2.0.0"
gem "webrat", ">= 0.7.2"
end
I have not added the file show.html.erb to the views/messages but i should be getting another error , not this
The strange thing is that this worked on my machine some time before. I deleted the project and created another one and now it just wouldn't work
I had issued these commands after editing the gemfile
bundle install
script/rails generate rspec:install
rake db:migrate
For me the answer was to change
describe "messages/show.html.erb" do
to
describe "messages/show.html.erb", type: :view do
If your show.html.erb_spec.rb file contains:
require 'spec_helper.rb'
try changing it to:
require 'rails_helper.rb'
Explanation:
https://www.relishapp.com/rspec/rspec-rails/docs/upgrade
It looks like you're missing a closing quote on the first line of your code:
require 'spec_helper
you could change your test like following:
require 'spec_helper'
describe "messages/show.html.erb" do
it "displays the text attribute of the message" do
FactoryGirl.create(:message) # or an other test data creator gem
visit "/messages/1"
page.should have_content("Hello world!")
end
end
I just upgraded to Capybara 2.0.0.beta4 with rspec-rails 2.11.4 and I moved my request spec (I only have one) to spec/features as advised by the Capybara-Readme in the RSpec-Rails repository.
When I run the tests now it does not find any paths. So for the following test block:
it "should be able to access the signup page through the front page" do
visit root_path
click_link "Signup For Free Now"
page.should have_content("Signup")
end
I get the error message:
Failure/Error: visit root_path
NameError: undefined local variable or method `root_path' for #<RSpec...>
When I try to run the test with visit "/" it works fine. Other gem versions are:
rails 3.2.1
rspec 2.11.0
rack-test 0.6.2
Any ideas for a reason for the path problem?
Running "bundle update rspec-rails" to get version 2.12.0 solved the same issue for me.
ruby-1.9.2-p180 :007 > Factory.define :user do |user|
ruby-1.9.2-p180 :008 > user.email "user#example.com"
ruby-1.9.2-p180 :009?> user.password "foobar"
ruby-1.9.2-p180 :010?> user.password_confirmation "foobar"
ruby-1.9.2-p180 :011?> end
NameError: uninitialized constant Factory
My Gemfile:
group :test do
gem "rspec-rails"
gem 'webrat', '0.7.1'
gem 'spork', '0.9.0.rc4'
gem 'factory_girl_rails'
end
Even tough it seems I have everything as it should, I keep getting that error. I also have factories.rb created.
Thanks
I suppose you try in console in development environment. But you add the Factory gem only in test environment.
If you want access to Factory_girl in development use in your Gemfile :
group :test, :development do
gem 'factory_girl_rails'
end
Or if you want test your Factory launch your console in test environment :
rails c test
We had a similar problem on our end, rake spec seemed to be randomly failing with the error uninitialized constant FactoryGirl. The error was random -> coming and going. We went back half a dozen git commits to try and resolve it. In the end it was a silly mistake.
The fundamental problem is RAILS_ENV is set to development. It needs to be set to test when you run rake spec.
Address by:
Making certain that we are running rake spec in the RAILS_ENV test environment and its exported/sourced properly. To never confuse our environemnts, we modified zsh $RPROMPT env variable to show the current env.
export RPROMPT="[%{$fg_no_bold[yellow]%}$RAILS_ENV%{$reset_color%}]"
Require FactoryGirl in the spec ruby files gave a much better error message. At least rspec would run vs just fail outright this way when the environment was wrong. We also updated our gemfile to make sure factory_girl_rails and factory_girl were loaded both for development and testing.
Now we just run rpsec using the gem guard in a dedicated terminal with the proper RAILS_ENV set.
Its one of those gotchas.
We're running Ruby 1.9.3, Rails 3.2.9, Rspec 2.12, factory_girl 4.1.
I also ran into this while I was doing the Hartl tutorial.
If you are using "Spork" to speed up your tests and it is running in the background when you add the FactoryGirl code, you will need to stop it and restart it.
You should also change Factory.define to FactoryGirl.define, like in this example
FactoryGirl.define do
factory :user do
name 'John Doe'
date_of_birth { 21.years.ago }
end
end
from the factory_girl documentation
I'm having a hard time getting a simple file upload test working. I'm using Rails 3.0.0 on ruby 1.9.2 with Cucumber and Capybara.
View:
<%= form_tag "/upload/create", :multipart => true do %>
<label for="file">File to Upload:</label>
<%= file_field_tag "file" %>
<%= submit_tag "Upload" %>
<% end %>
Cucumber Step:
When /^I upload the basic file$/ do
visit path_to("upload")
path = File.join(::Rails.root, "somefile")
attach_file("file", path)
click_button("Upload")
end
In my controller, i have commented out everything except for:
def create
file = params[:file]
end
Gemfile snippet:
group :development, :test do
# testing with specs
gem "ZenTest", ">= 4.3.3"
gem "autotest"
gem "rspec-rails", ">= 2.0.0.beta.19", :git => "git://github.com/rspec/rspec-rails.git"
gem "rspec", :git => "git://github.com/rspec/rspec.git"
gem "rspec-core", :git => "git://github.com/rspec/rspec-core.git"
gem "rspec-expectations", :git => "git://github.com/rspec/rspec-expectations.git"
gem "rspec-mocks", :git => "git://github.com/rspec/rspec-mocks.git"
# cucumber stuff
gem 'capybara'
gem 'database_cleaner'
gem 'cucumber-rails'
gem 'cucumber'
gem 'spork'
gem 'launchy' # So you can do Then show me the page
gem 'escape_utils' # needed to fix Cucumber - http://crimpycode.brennonbortz.com/?p=42
end
When I try to run the test, I receive:
(::) failed steps (::)
bad content body (EOFError)
<internal:prelude>:10:in `synchronize'
I appreciate any help or insight. Thanks.
This turned out to be an issue with rack-test and it probably won't be a problem for most until more people adopt Rails3 and Ruby 1.9.x.
Upgrading rack-test to the current master branch fixed the problem.
I'm not sure when rack-test will include these changes in the gem.
See also:
groups.google.com/group/cukes/browse_thread/thread/5028306893c2c54a
I don't have an answer but working on the same problem in the same environ - cukes, capybara, rails 3, 1.9.2.... if I figure this out will let you know. Have you thought of posting on the cucumber google group or the Rails google group? If you don't once I get my act together and cant figure out will post to one of these.
Also, it seems that webrat has the method for attach_file() and thus when I generated cucumber without capybara it had a corollary method in web_steps.rb, but after I added capybara and regenerated cucumber it was gone....