rspec show failure even when the condition is fulfilled - ruby-on-rails

I am using rspec to validate if a string exist in a page. I am new to rails and following tutorial http://ruby.railstutorial.org/chapters/static-pages#top
Is there anything I am missing?
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
In my home page app/views/static_pages/home.html.erb I have following code.
<h1>Sample App</h1>
Command I am using to validate the condition.
bundle exec rspec spec/requests/static_pages_spec.rb
Test is failing don't know why.
←[31mF←[0m
Failures:
1) Static pages Home page should have the content 'Sample App'
←[31mFailure/Error:←[0m ←[31mvisit '/static_pages/home'←[0m
←[31mNoMethodError←[0m:
←[31mundefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1::
Nested_1:0x4067a60>←[0m
←[36m # ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top(required)>'←[0m
Finished in 0.023 seconds
←[31m1 example, 1 failure←[0m
Failed examples:
←[31mrspec ./spec/requests/static_pages_spec.rb:7←[0m ←[36m# Static pages Home page should have the content 'Sample App'←[0m
Randomized with seed 44188

Going by what the tutorial has you do and without your Gemfile to double check, I believe you are experiencing a conflict between Capybara 2.0 and RSpec on the visit keyword. You have two options for a solution (don't need to do both):
Option 1: Move your test from the spec/requests folder into the spec/features folder.
or
Option 2: Use the keyword feature instead of describe.
This conflict is caused by a change to Capybara noted at the top of this gist (second paragraph): https://gist.github.com/jnicklas/3980553. Quoted:
Notably, we changed the :type that Capybara assumes your specs run under in RSpec to :feature (previously it was :request). The latest release of spec/features. Alternatively you can use the Capybara Feature DSL (feature instead of describe), which should work without any additional tweaking. If you see errors like undefined method visit, then you're probably encountering this issue. If you're including modules into :request specs, you will probably need to change that to :feature.

Related

Routes Increment when I Run Rspec feature

So, for some reason my routes increment by 11 when I run my comment feature test.
Exhibit A: I hit save on my comment_spec.rb
RSpec.feature "Adding comments to movies" do
before do
#kyle = Admin.create(email: "kyle#example.com", password: "password")
#jill = Member.create(email: "jill#example.com", password: "password")
#movie = Movie.create!(title: "First movie", synopsis: "Synopsis of first movie", year_released: '2000', admin: #kyle)
end
scenario "permits a signed in member to write a comment" do
login_as(#jill, scope: :member)
visit "/"
click_link #movie.title
fill_in "New Review", with: "An awesome movie"
click_button "Add Review"
expect(page).to have_content("Review has been created")
expect(page).to have_content("An awesome movie")
# must implement a nested route in order for this to work.
expect(current_path).to eq(movie_path(#movie.comments.last.id))
end
end
Then the test passes
moviesmoviesmovies/spec/features/comments_spec.rb:3)
Finished in 0.99879 seconds (files took 2.4 seconds to load) 1
example, 0 failures
Now, I hit enter in the terminal and get this:
Adding comments to movies permits a signed in member to write a comment
Failure/Error: expect(current_path).to eq(movie_path(#movie.comments.last.id))
expected: "/movies/2"
got: "/movies/4"
(compared using ==)
# ./spec/features/comments_spec.rb:26:in `block (2 levels) in <top (required)>'
Then I save the comment_spec.rb again and get this:
Adding comments to movies permits a signed in member to write a comment
Failure/Error: expect(current_path).to eq(movie_path(#movie.comments.last.id))
expected: "/movies/3"
got: "/movies/15"
(compared using ==)
# ./spec/features/comments_spec.rb:26:in `block (2 levels) in <top (required)>'
After this happens, I run:
bundle exec rails db:test:prepare
Then the tests pass again, but the above repeats. What in the world is making this happen? haha, but really?
My GitHub if needed.
The issue you're having is that your database isn't being reset after each feature test, as you likely expect it is. Specifically, the problem is with Capybara and the config.use_transactional_fixtures = true line which is set in your spec config.
Capybara effectively tests your application as an external process using a virtual browser. This means that your capybara tests only have visibility into the client-side behavior of your app (i.e. the page variable). In other words, from Capybara's perspective your app is a "Black box". Therefore, capaybara doesn't see controller objects like your session, params, or request variables, nor can it see or control the specific database transactions which occur during testing.
Rather than relying on transactional fixtures, consider using database cleaner, which manually resets the database after each feature test. You can find the gem here: https://github.com/DatabaseCleaner/database_cleaner. Make sure you follow the directions on integrating with Capybara: https://github.com/DatabaseCleaner/database_cleaner#rspec-with-capybara-example.

Capybara/selenium browser refreshed with the blank page

After adding selenium-webdriver gem, when started capybara test, browser started to show home page and after it, refreshed with the blank page.
It cause to fail my tests that previously passed. Showing Capybara::ElementNotFound error.
I use capybara 2.2.1; selenium 2.42.0; ruby 2.1.0; rails 4.1.0; rspec 3.0
Here is my test:
require 'spec_helper'
describe "Navigation" do
it "changing active menu element depending on route", js: true do
visit '/'
first(:link, 'WHAT IS DREAMDO?').click
expect(find('.navigation').find('.active').find('a').text).to eq('WHAT IS DREAMDO?')
first(:link, 'DISCOVER DREAMS').click
expect(find('.navigation').find('.active').find('a').text).to eq('DISCOVER DREAMS')
first(:link, 'DREAMDO WEEKLY').click
expect(find('.dreamdo-menu-items').find('.active').find('a').text).to eq('Dreamdo Weekly')
end
end
describe "Search", js: true do
it "checks for the availability and search results" do
search_text = find(:xpath, '//input[#id="search_text"]').set("chocolate")
search_text.native.send_keys :return
page.should have_content "chocolate"
end
end
Could you, please, recommend me what to do?
Actually, it is not good to use selenium, because it can't run multiple instances:
http://robots.thoughtbot.com/capybara-webkit
Try to use capybara-webkit, hope it will solve your problem.

Rails Capybara not working

I am trying to run a simple test using rspec and capybara:
describe "Create" do
visit new_client_path
page.should have_selector('h1', text: "New Client")
end
However i get error:
undefined local variable or method `visit'
And if i remove the visit line i get this error:
undefined local variable or method `page'
I have included the DSL in the spec_helper file.
what is the problem?
Thanks
It seems that you simply forgot to put your test inside the it block:
describe "Create" do
it "does something" do
visit new_client_path
page.should have_selector('h1', text: "New Client")
end
end
Assuming you are using Rails 3 and RSpec, have you placed your specs in features instead of requests? You shouldn't need to include the DSL if you are using features directory.
Also check that you have latest version of the gems.
From https://github.com/jnicklas/capybara:
Using Capybara with RSpec
Load RSpec 2.x support by adding the following line (typically to your
spec_helper.rb file):
require 'capybara/rspec' If you are using Rails, put your Capybara
specs in spec/features.

Capybara NoMethodError: undefined method `visit' for

Thank for response.
I getting error in application Git application with the error
Failures:
1) Page pages page creation with invalid information should not create a page
Failure/Error: before(:each) { visit new_admin_page_path }
NoMethodError:
undefined method `visit' for #<Page:0x00000005094bb0>
# ./spec/requests/page_request_spec.rb:13:in `block (3 levels) in <top (required)>'
2) Page pages page creation with invalid information error messages
Failure/Error: before(:each) { visit new_admin_page_path }
NoMethodError:
undefined method `visit' for #<Page:0x000000051671f0>
# ./spec/requests/page_request_spec.rb:13:in `block (3 levels) in <top (required)>'
Finished in 1.21 seconds
14 examples, 2 failures
What I've missed ?
I cloned your repository and ran the spec. The problem is that you are using the term page for two different things, the Capybara page (subject { page }) and your model with the name Page (which you also name page). Those two names conflict, which is why rspec is complaining that there is no method visit for #<Page:0x00000005094bb0>.
To solve this just rename the page you create in the initial let:
let(:mypage) { Factory.create(:page) }
That will avoid the conflict by naming your page model mypage instead of page.
There are other issues however. Your user factory does not use a sequence for assigning emails, so you get a validation error because it says the email is already taken.
I changed your user factory to this:
sequence(:email) { |n| "abc#{n}#example.com"}
factory :user do
email
password "foobar"
password_confirmation "foobar"
...
That fixes the validation error, but you now get another error. In the line in your first spec, you should be checking that count does not change on the class Page, not the record page (or mypage).
So change that to:
expect { click_button "Create Page" }.not_to change(Page, :count)
This will still leave you with errors, because you also do not have any create action for your PagesController. From what you wrote, it seems like the information is invalid so it should not even get to this action. I don't know exactly what you're trying to do here so I'll leave the rest for you to figure out.
If you recently upgraded to Capybara 2.0 & your integration tests were working with previous version of Capybara, you may try renaming requests directory to features. Check http://www.codelearn.org/blog/how-to-make-old-integration-tests-work-with-capybara-2-0
Add the following to your spec/spec_helper.rb (in your Spork.prefork block):
require 'capybara/rspec'
require 'capybara/rails'

RSpec fails for no obvious reason

So I'm using rspec to test my code as I'm going through the Rails Tutorial, and I keep getting this error when I test the code in listing 3.20. Everything checks out when I look at it with my eyeball, but RSpec doesn't seem to like it.
(Note that I just did one of the pages, not all three because they all give the same error)
james#tristan:~/rails_projects/sample_app$
rspec
spec/controllers/pages_controller_spec.rb
F...
Failures:
1) PagesController should have the
right title
Failure/Error: response.should have_selector("title",
expected following output to contain a | Home tag:
# ./spec/controllers/pages_controller_spec.rb:13:in
`block (2 levels) in '
Finished in 0.97999 seconds 4
examples, 1 failure
james#tristan:~/rails_projects/sample_app$
At the top of that spec file it says:
before(:each) do
#
# Define #base_title here.
#
end
Does your spec assign a value to #base_title?

Resources