I'm testing rails app with the Cucumber and Capybara/rack_test.
I have following step definition:
Then /^I should see '([^']*)'$/ do |content|
visit about_path
response.should have_content(content)
end
where about_path maps to /about.
Every time my story fails with the message
Feature: Some Great Feature
So that I can blah-blah
As a Blah-blah
I want blah-blah
Scenario: Show project label on about page # features/about.feature:7
Given I'm on '/about' page # features/step_definitions/about_steps.rb:1
Then I should see 'About' # features/step_definitions/about_steps.rb:5
expected there to be content "About" in "" (RSpec::Expectations::ExpectationNotMetError)
./features/step_definitions/about_steps.rb:7:in `/^I should see '([^']*)'$/'
features/about.feature:9:in `Then I should see 'About''
Failing Scenarios:
cucumber features/about.feature:7 # Scenario: Show project label on about page
1 scenario (1 failed)
2 steps (1 failed, 1 passed)
Note that actual content is empty. It is empty with every path I've tried.
It is strange because rspecs passes.
Is there anything I could try? Tell me if you need additional information.
You shouldn't use response to get to the content. Try with page instead.
Related
I have
allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(user)
But for some reason when I print page.body I am getting
"Rack session data \"session_id\" : \"{session_id}\" \"user_id\" : 1 \"uid\" : \"{user uid}\"
Instead of HTML for a page. I'm very confused by this behavior. I'm on capybara 2.4.4
Did you visit the correct page?
The command for visiting a given page is visit <insert path here>. Make sure you visit your desired page before trying to retrieve its body.
I have a Rails 4.2 application....I was adding content compression via this thoughtbot blog post, but I get an error such as:
undefined method `get' for #<RSpec::ExampleGroups::Compression:0x00000009aa4cc8>
Perusing over the capybara docs, it seems like you shouldn't be using get. Any idea how to test the below then in Rails 4?
# spec/integration/compression_spec.rb
require 'spec_helper'
feature 'Compression' do
scenario "a visitor has a browser that supports compression" do
['deflate','gzip', 'deflate,gzip','gzip,deflate'].each do|compression_method|
get root_path, {}, {'HTTP_ACCEPT_ENCODING' => compression_method }
response.headers['Content-Encoding'].should be
end
end
scenario "a visitor's browser does not support compression" do
get root_path
response.headers['Content-Encoding'].should_not be
end
end
In a capybara test you would use visit not get (as described here), but that answer won't actually help you because the test you've written above is not an integration test, it's a controller test.
Move it to spec/controllers and use the controller-specific helpers describe/context/it etc. to construct your tests for your controller. You can set the headers and do the sorts of checks that you're doing in the code you're showing.
I am using a Cucumber-Capybara combination for testing. I am not using Rails application but a simple bundler application.
I want to test some dynamic URLs for my front-end application. For example, the URL for item-show page is:
/suitability/items/197/
(Assertion: Then I go to suitability-item-show page)
In the above URL, 197 is an :id. I want to test similar pages which contains dynamic data.
I have tried following two ways:
Then(/^I should be on suitability\-item\-show page$/) do
visit '/suitability/items/:id/'
end
and
Then(/^I should be on suitability\-item\-show page$/) do
visit "/suitability/items/#{:id}/"
end
But they didn't work out for me
Please help me with the solution to this.
If var="id", then
visit "/suitability/items/#{var}/"
should work.
I'm not getting how u got ':id'...can u throw some more light on that
I'm getting started with rspec, capybara, etc, and I want to do some test driven development in Rails.
The specification I'm working with has very precise definitions for the appearance of the headers and the footers, and I figure that these are a good place to start learning.
For the footer, I want to have the following rule:
If the user is logged in, then the header contains just a logo image. The logo image should be a link to the user's landing page, which is determined by the rights the user has. If the user is not logged in, then the image is not a link, and four other links should appear in the footer as well, in a table.
Coding this is actually fairly straightfoward in erb, but I'm trying to Do The Right Thing, and make a series of tests here. My problems is that I can't seem to be able to test whether or not an image is shown on the screen. I've read the rspec book, but I don't see where it says something like 'shows this image found in the assets directory.'
So my setup is, in the views/layouts directory,
_header.html.erb
_footer.html.erb
_shim.html.erb
application.html.erb
I would think that I could test the footer partial directly, using something like:
require "spec_helper"
describe "rendering views/layouts/_footer.html.erb" do
#from https://www.relishapp.com/rspec/rspec-rails/v/2-8/docs/view-specs/view-spec
it "shows the logo" do
render :template => "layouts/_footer.html.erb"
rendered.should =~ "/images/mainlogo.png"
end
describe "rendering views/layouts/_footer.html.erb as admin" do
before do
FactoryGirl.create(:admin_user)
end
it "links to landing from the logo"
render :template => "layouts/_footer.html.erb"
rendered.should contain("link/to/admin/landing")
end
end
#repeat landing tests for various user types
And then, in other pages, I can simply test for the presence of the footer itself using something like
it should contain("footer")
My problem is, I can't even get off the ground to check to see if the image has been shown, much less if the image matches the right one in the assets directory. What should I be doing here?
The above code for testing the presence of the image (just the first describe/it block, not the stuff with the 'as admin' or the 'should contain') gives me the following warning and error:
DEPRECATION WARNING: Passing a template handler in the template name is deprecated.
TypeError:
type mismatch: String given
The first is probably due to me using syntax I don't understand, but the second seems to suggest that the comparison is with strings rather than assets. What's the syntax to compare images? Is there one?
Your type mismatch error is because you're passing a string to =~ which is a regular expression matcher. You can either change the string to a regular expression, or use the include instead. I'd go with include just because it's more readable:
rendered.should include("/images/mainlogo.png")
To get rid of the deprecation warning, just remove the .erb from the template name:
render :template => "layouts/_footer.html"
I'm writing an integration test for a rails application using webrat. After filling out a form, the user presses submit and an account is created.
click_button "Submit"
assert_contain "Your Account Has Been Created"
However, the test fails:
expected the following element's content to include "Your Account Has Been Created":
You are being redirected.
<false> is not true.
Normally to follow a redirect I would use post_via_redirect, but from just looking at Webrat's examples, click_button followed by assert_contain should work
I just started using Webrat, so am I missing something obvious here? Why am I stuck with the redirect response?
Thanks!
Deb
With a new Rails 3 app, I also had this problem testing a simple method which included a redirect_to call in the controller. The method itself worked fine, but Webrat would return the "You are being redirected." response.
Adding in a 'Then show me the page' step in cucumber (so the page that webrat sees opens in the browser) showed the 'You are being redirected." response with a link to an example.org link.
Based on this I discovered Yannimac's patch ( http://groups.google.com/group/webrat/browse_thread/thread/fb5ff3fccd97f3df ):
#/lib/webrat/core/session.rb
#starting at line 288
def current_host
- URI.parse(current_url).host || #custom_headers["Host"] || "www.example.com"
+ URI.parse(current_url).host || #custom_headers["Host"] || default_current_host
end
+ def default_current_host
+ adapter.class==Webrat::RackAdapter ? "example.org" : "www.example.com"
+ end
Making these changes fixed the issue, so redirect_to calls with Webrat now work correctly.
There are some issues with rails 3 and webrat. Please see:
http://baldowl.github.com/2010/12/06/coercing-cucumber-and-webrat-to-cooperate.html
Do you have any authentication in your apps? I presume the redirection is because of you have not been authenticated. If my assumption is right, write a setup to login first with Webrat.
Here is the gist with exactly what you need to do to solve this problem.
https://gist.github.com/752766