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.
Related
Hey guys I am running into a bit of trouble with a simple view test written using Rspec. The case is pretty simple. I want to render a page that should display a message asking the customer if he/she is new.
The page contains several subsections that contain translations. The problem is that Rspec puts a "test" in this translation path, so the correct translation is not found.
Rspec test:
require "rails_helper"
RSpec.describe 'checkout/start/index' do
context "with no logged in customer" do
it "should render the #index page and display the registration and login form" do
render
expect(rendered).to match(/Sie sind neu bei uns?/)
end
end
end
Response:
Randomized with seed 40265
checkout/start/index
with no logged in customer
calls #index and displays the registration and login form (FAILED - 1)
Failures:
1) checkout/start/index with no logged in customer calls #index and displays the registration and login form
Failure/Error: <%= t(".checkout_#{controller_name}_sub_headline") %>
ActionView::Template::Error:
translation missing: de.checkout.shared.sub_headline.checkout_test_sub_headline
# /home/arthur/.rvm/gems/ruby-3.1.2/gems/i18n-1.12.0/lib/i18n.rb:394:in `handle_exception'
# /home/arthur/.rvm/gems/ruby-3.1.2/gems/i18n-1.12.0/lib/i18n.rb:367:in `translate_key'
# /home/arthur/.rvm/gems/ruby-3.1.2/gems/i18n-1.12.0/lib/i18n.rb:222:in `translate'
# ./app/views/checkout/shared/_sub_headline.html.erb:6:in `_app_views_checkout_shared__sub_headline_html_erb__648894094841044844_23060'
# ./app/views/checkout/start/index.html.erb:8:in `_app_views_checkout_start_index_html_erb__2185710400981636407_23000'
# ./spec/views/checkout/start_spec.rb:10:in `block (3 levels) in <top (required)>'
# ------------------
# --- Caused by: ---
# I18n::MissingTranslationData:
# translation missing: de.checkout.shared.sub_headline.checkout_test_sub_headline
# /home/arthur/.rvm/gems/ruby-3.1.2/gems/i18n-1.12.0/lib/i18n.rb:394:in `handle_exception'
Top 1 slowest examples (0.03185 seconds, 6.4% of total time):
checkout/start/index with no logged in customer calls #index and displays the registration and login form
0.03185 seconds ./spec/views/checkout/start_spec.rb:9
Finished in 0.49579 seconds (files took 1.69 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/views/checkout/start_spec.rb:9 # checkout/start/index with no logged in customer calls #index and displays the registration and login form
Randomized with seed 40265
Is there any way to override this path for the test, or any other way to tell Rspec where to look for the correct translation?
Many thanks in advance
You have created a translation that's dependent upon the controller_name, which as you're seeing is different in your test environment because of the way Rails view tests work. If you need that in your code, then in your test I would stub it out so you're providing what your code expects. I'm not sure, but that's probably something like allow(helper).to receive(:controller_name) { 'your_controller_name' }
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.
I followed a few tutorials and docs of FactoryGirl to use with RSpec. Currently I get one error when trying to use FactoryGirl.create:
describe "GenericRecipesController" do
describe "GET 'index'" do
it "displays list of generic recipes" do
generic_recipe = FactoryGirl.create(:generic_recipe)
visit '/recipe'
response.should be_success
end
end
end
And the error:
GenericRecipesController GET 'index' displays list of generic recipes
Failure/Error: generic_recipe = FactoryGirl.create(:generic_recipe)
NameError:
uninitialized constant GenericRecipe
# ./spec/integration/generic_recipes_spec.rb:8:in `block (3 levels) in <top (required)>'
The rest of code is there.
You can try this:
factory :generic_recipe, class: EdibleRecipe::GenericRecipe do
# ...
end
I think problem in a nesting model in module
Upd: delete file /spec/factories.rb, in file /spec/support/factories.rb make
factory :generic_recipe, class: EdibleRecipe::GenericRecipe do
When you will run tests, probably will see 'can not load table'. Make
rake db:migrate RAILS_ENV=test
and try again.
You don't seem to have a GenericRecipe model in your app. Factory Girl is looking for a Model called GenericReciper and can't find it.
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'
i'm new to rspec and ruby...
i have the following code in one of course_rspec.rb
...
it "check for active courses after enabling a course" do
course = Course.create(:title => "Testing", :description => "Testing")
course.enabled = true
course.save
active_courses = Course.where(:enabled => true)
active_courses.length.should eql 1
end
...
i get the following error
1) Course check for active courses after enabling a course
Failure/Error: active_courses.length.should eql 1
expected: 1
got: 0
(compared using eql?)
# ./course_spec.rb:86:in `block (2 levels) in <top (required)>'
why is the Course.where method return no objects? when i do this in rails console it works fine.
thanks for your help.
Are you running rails console in the test mode or in the development mode? Keep in mind that rails for testing and developing an application uses different databases.
Make sure that before the test all records are prepared and stored in the database. For this purpose you could use fixtures or factories (factory_girl for example).
ps.
Instead active_courses.length.should eql 1 you can use active_courses.should have(1).item which is more readable.