Capybara undefined method `expect' for Capybara:Module - ruby-on-rails

I am using /ruby-1.9.3-p545/gems/capybara-2.4.4 and /ruby-1.9.3-p545/gems/selenium-webdriver-2.44.0.
My Capybara config:
config.default_driver = :selenium
config.run_server = false
config.default_selector = :css
config.default_wait_time = 60
I can assure you following methods work fine
Capybara.visit
find(:css, "#checkbox-selectall").set(true)
The only thing that is not working is the 'expect'
expect(page).to have_css('.screen-reader-text')
or
Capybara.expect(page).to have_css('.screen-reader-text')
I get the following error
undefined method `expect' for Capybara:Module (NoMethodError)
I can clearly see in the documentation that this version of capybara does support the expect method but i am unable to understand why it is saying NoMethodError as if i am not calling it from the right scope or something.
Capybara Docs: http://www.rubydoc.info/gems/capybara/Capybara

Install RSpec -- put it in your gemfile, next to Capybara
gem 'rspec'
run bundle install
Generate an install
rails generate rspec:install, more info here https://github.com/rspec/rspec-rails
Be sure to include capybara and your capybara settings in the new helper file (spec_helper or rails_helper).

Related

Rails 6.1 upgrade: undefined method `assert_nothing_raised'

I'm running into this error when upgrading from Rails 6.0.3 to 6.1:
NoMethodError:
undefined method `assert_nothing_raised' for #<RSpec::ExampleGroups::EmailJob:0x00005572d8a00758>
Did you mean? assert_raises
This happens every time a test calls perform_enqueued_jobs.
I'm using RSpec 3.9.
Apparently assert_nothing_raised is a helper method defined by ActiveSupport. I was able to solve this issue by explicitly including the helper in spec/rails_helper.rb:
RSpec.configure do |config|
config.include(ActiveSupport::Testing::Assertions)
# ...

Devise 3.2 + Rspec 3

I'm trying to build some tests with Rspec and I'm getting the following error:
Failure/Error: get :index
NoMethodError:
undefined method `authenticate!' for nil:NilClass
Then I made some searchs and I realized that I had to include this line in the spec_helper.rb:
RSpec.configure do |config|
config.include Devise::TestHelpers, type: :controller
end
It seems that this code worked for most people but not for me. Now i'm getting the following error:
/home/bruna/Dropbox/Rails/academico/spec/spec_helper.rb:18:in `block in <top (required)>': uninitialized constant Devise (NameError)
I think this might be some error in this version of rspec and/or devise. Has anyone seen this error? Please, help me :'(
RSpec 3 configuration is different from earlier versions, with the addition of a spec/rails_helper.rb file that must be included for each spec file. Add the Devise TestHelpers to the spec/rails_helper.rb file.
If you need more information about RSpec 3 configuration, I've written an RSpec Tutorial. There's also information about testing Devise with RSpec, including advice about session helpers for feature testing, in my Rails Devise Tutorial.

Setting up a new RoR project using Rspec and Capybara

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.

View helper methods not included for Devise views in rspec integration/request tests?

When I visit my sign in page in a browser everything works fine.
When I visit my sign in page in an rspec integration/request test, I get the following error:
ActionView::Template::Error:
undefined method `title' for #<#<Class:0x00000007af9180>:0x00000007af32a8>
The title method is used by the view and defined in ApplicationHelper which devise seems to find when using the browser. However, during rspec integration tests, devise is unable to find the helper method.
Is there anything I should be stubbing? It seems wrong to be stubbing in integration tests. Any other ideas?
(This question is not about how to include devise helpers in integration tests. I'm manually filling in the sign in forms to authenticate).
Looks like this issue. (in some cases related to ActiveAdmin https://github.com/gregbell/active_admin/wiki/Use-spork)
Here I found a hack that works for me (REE 1.8.7, Rails 3.1, Capybara, Devise, active_admin).
However, this is not likely to be merged, so I forked spork-rails to here with that patch applied. And as you probably know I can point my Gemfile to that repo:
gem 'spork-rails', :git => "git://github.com/chopmo/spork-rails.git"
Not optimal but it gets the job done for now.
I had a similar problem using Cucumber when I installed devise:
undefined local variable or method `flash_block' for #<#<Class:0x007ffd0a28dae8>:0x007ffd0b2f6d58> (ActionView::Template::Error)
I solved it by including the module in env.rb
Spork.prefork do
include FlashBlockHelper
I hope this helps.
Inside /spec/support create devise.rb with this:
RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
end
Make sure your spec_helper.rb includes:
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
and that your specs have:
require 'spec_helper'

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