I'm searching for a CSS class. My HAML is:
.friends
= image_tag 'news-face.png'
%p 15
My test is:
it "Should show friends count on popular feed" do
visit '/'
page.should have_css('.friends')
end
I'm getting an error:
Failure/Error: page.should have_css('.friends')
expected css ".friends" to return something
I've tried should, should_not, & have_selector and can't figure out what's wrong.
It seems that you don't have such element on your page. The best way to figure it out by yourself is to install:
# in your Gemfile
gem 'launchy'
and then just use:
# in Cucumber step
it "Should show friends count on popular feed" do
visit '/'
save_and_open_page
page.should have_css('.friends')
end
With this gem and command you will be able to render this page from your test.
Maybe you would use has_css selector.
Related
I am having a problem including a should have_selector problem with rspec:
This is my code:
describe "GET 'home'" do
it "returns http success" do
get 'home'
expect(response).to be_success
end
it "should have the right title" do
should have_selector("title",
:content => "Ruby on Rails Tutorial Sample App | Home")
end
end
I have included the following at the top:
RSpec.describe PagesController, :type => :controller do
render_views
My html5 has the following:
<title>Ruby on Rails Tutorial Sample App | Home</title>
and I get an error message saying:
Failures:
1) PagesController GET 'home' should have the right title
Failure/Error: should have_selector("title",
expected #<PagesController:0x007fceef586a90> to respond to `has_selector?`
# ./spec/controllers/pages_controller_spec.rb:14:in `block (3 levels) in <top (required)>
Can someone help with that?
rspec -v
3.0.2
rails 4.1.1
thank you in advance.
Rspec 3 does not include the capybara matchers in controller specs by default. You can change this for an individual spec by doing
include Capybara::RSpecMatchers
Or, in your spec helper
config.include Capybara::RSpecMatchers, :type => :controller
Your next issue is that recent versions of capybara don't allow you to test for the presence of invisible elements by default, and the title element is considered to be invisible. You should use the have_title matcher instead.
Hollo rubyist pal!
I had this problem and that was because of two things, first I did not use capybara gem, and second have_selector only accepts one of :count, :minimum, :maximum, :between, :text, :visible, :exact, :match, :wait keys and does not understand :content.
I am sure you solved the problem, but for those who have started to learn Ruby on Rails recently and encountered such a problem I should say to eleminate it first put capybara gem in your Gemfile as below:
group :development, :test do
...
gem 'capybara'
end
and run command
bundle install
then to test that your view has a specific title, in pages_controller_spec.rb file write
describe "GET #home" do
...
it "should have the right title" do
get :home
expect(response.body).to have_selector('title', :text => 'Ruby on Rails rocks')
end
end
I hope it would help someone.
Cheerio!
Checking content is view spec...
And controller spec means checking response status/path, render page/partials, instance variable filters params.
When running my specs i get errors where Capybara couldn't find elements based on their labels and such. Then i thought i wanted the HTML, or somehow debug the errors.
This is the /features/merge.feature
#Just a test!
Scenario: Cannot merge articles when not admin
Given I am not an admin
When I am on the edit article page
Then I should not see the "merge articles" form
And this is: /feature/step_definitions/merge.rb - and it only contains "Given" statements!
Given /am not an admin$/ do
visit '/accounts/login'
puts "TEZT"
puts page.native
puts page.native.text
fill_in 'user_login', :with => 'editor_rico'
fill_in 'user_password', :with => 'a'
click_button 'Login'
end
Given /^I am an admin$/ do
visit '/accounts/login'
fill_in 'user_login', :with => 'admin_rico'
fill_in 'user_password', :with => 'a'
click_button 'Login'
end
Then i get this error:
Scenario: Cannot merge articles when not admin
# features/article_merging.feature:20
Given I am not an admin
#features/step_definitions/article_steps.rb:39
cannot fill in, no text field, text area or password field with id, name, or label
'user_login' found (Capybara::ElementNotFound)
(eval):2:in `fill_in'
./features/step_definitions/article_steps.rb:44:in `/am not an admin$/'
features/article_merging.feature:21:in `Given I am not an admin'
Havin a rough time working with Cucumber here ..
1) Why do i get the above
2) How do i debug it, i wanna see the HTML code!
This errors appears when your page HTML does not contain any input or textarea element with name, id or label 'user_login'. If you add the launchy gem to your Gemfile you will be able to see how your page looks:
In your Gemfile:
group :test do
gem 'launchy'
gem 'capybara'
...
end
Then, inside your feature/step_definitions/merge.rb file:
Given /^I am not an admin$/ do
visit '/accounts/login'
save_and_open_page
end
The save_and_open_page will open the current page in your browser, so then you can inspect the HTML and see what it looks like.
Hope it helps! :)
It think the issue with the above code is that 'page' doesn't become available until after you end the section.
Try putting your 'puts' into the 'When'or 'Then' step
You can query page as follows (assuming you're using RSpec)
page.should have_content 'foo'
page.should have_selector 'h1', text: => 'Hello dave'
Hope this helps
You can also see the HTML response by printing page.body to stdout.
Given /^I am not an admin$/ do
visit '/accounts/login'
puts page.body
end
I'm currently going through the Ruby on Rails Tutorial by Michael Hartl, and in Section 3.2.2 of the tutorial, he talks about using RSpec to test for the titles of the page. My tests (which I wrote myself, following his tutorial) kept failing the title tests (it can find the tag, but the title is always a blank string), and some searching on Google told me I needed to include the function render_views to pass the tests. My problem is that no matter what I try, RSpec returns a NoMethodError for render_views. The things I've tried:
config.render_views in spec_helper.rb
describe "Static Pages" do
render_views
...
end in the spec file
describe "Static Pages" do
RSpec::Rails::ViewRendering.render_views
...
end in the spec file
All of them return a NoMethodError.
I'm not following his Gemfile exactly, so the versions of Ruby and the relevant gems are:
Ruby: 1.9.3-p125
rails: 3.2.9
rspec: 2.12.0
rspec-rails: 2.12.0
capycabra: 2.0.1
I'm working in RubyMine 4.5.4, on Windows 7. Running the test in a command prompt returns the same errors however. The spec file is static_pages_spec.rb and is located in spec/requests
The code in my static_pages_spec.rb:
require_relative '../spec_helper'
describe "Static Pages" do
#RSpec::Rails::ViewRendering.render_views, returns NoMethodError
#render_views, returns NameError
describe "Home Page" do
it "should have the h1 'Sample App'" do
visit '/static_pages/home'
page.should have_selector('h1', :text => "Sample App")
end
it "should have the title 'Home'" do
visit '/static_pages/home'
page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App | Home")
end
end
describe "Help Page" do
it "should have the h1 'Help'" do
visit '/static_pages/help'
page.should have_selector('h1', :text => "Help")
end
it "should have the title 'Help'" do
visit '/static_pages/help'
page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App | Help")
end
end
describe "About Page" do
it "should have the h1 'About Us'" do
visit '/static_pages/about'
page.should have_selector('h1', :text => "About Us")
end
it "should have the title 'About Us'" do
visit '/static_pages/about'
page.should have_selector('title', :text => "Ruby on Rails Tutorial Sample App | About Us")
end
end
end
My spec_helper.rb is the spec_helper.rb automatically generated by creating running rails generate integration_test after creating the project with --no-test-framework, with these lines added in the config block:
# Fix NoMethodError for method 'visit'
config.include Capybara::DSL
config.include Capybara::RSpecMatchers
EDIT
After some testing, I've managed to get the tests to pass by changing the capybara version to 1.1.2, while still using rspec 2.12.0 and rspec-rails 2.12.0 and not using render_views. While I'm glad the tests are passing, it still doesn't solve the problem of the missing render_views method, which still crops up if I try to use it.
Well, after a bit more testing, the mystery of the missing render_views method is solved. To include the render views method, apparently config.include RSpec::Rails::ViewRendering in the spec_helper is needed before calling config.render_views in the spec_helper or render_views in the spec file.
Secondly, with regards to the tests passing or failing, the problem seems to be in Capybara itself. Apparently, it seems that the way the visit method is implemented in 1.1.2 and 2.0.1 is significantly different in what it returns; even after including render_views, the test still fails (without using config.include Capybara::RSpecMatchers). Using it seems to provide an insight into its implementation though; the failure message from Capybara on using the RSpecMatchers is that it is looking for CSS instead of HTML. I don't claim to fully understand why it fails, however, so if anyone can enlighten me as to why Capybara 2.0.1 causes the tests to fail while 1.1.2 allows the tests to pass, that would be great.
Capybara 2.0 requires all tests to be in spec/features, and not in spec/requests.
You can read all about it here.
I would suggest that you stick to the versions mentioned in Hartl's tutorial, atleast in your beginner days. Will help you get through the tutorial faster!
I am trying to test my rails app with rspec 2.10.0 + capybara 1.1.2. Here is my test file
require 'spec_helper'
describe AdminPanelController do
describe "index" do
it "should have return code 200" do
visit '/admin'
page.should have_content "hello"
#response.status.should be(200)
end
end
end
And here are test result
Failure/Error: page.should have_content "hello"
Capybara::ElementNotFound:
Unable to find xpath "/html"
I google about this issue but find only information that webrat can be a problem however i do not have this gem installed. Thanks for any suggestions.
Wrong type of test. This looks like a controller test, which does tests with methods like get and post and is in the spec/controllers folder. Request specs, which use capybara, reside in spec/requests. Run $ rails generate scaffold SomeModel to see how they each should look.
If you understood the above but would still like to use capybara for your controller test, modify your describe block:
describe AdminPanelController, :type => :request do
...
end
If I have something like <%= link_to "Help", help_path %> in the view file, how do I test the following using rspec?
It goes to help_path.
The content reads "Help".
I am currently using capybara.
edit
While answers below can work, I think I found a simpler way of doing this.
describe "some link on whatever page" do
subject { page }
before { visit whatever_path }
it { should have_link('Help', href: help_path) }
end
If you are using capybara then
my_link = find(:css, "a:contains('Help')")
my_link.click
and get page status, should be 200 or simply check
my_link.href
But also you could simply add routes specs to your integration specs in rspec. Like this to be always sure this routes exists
describe HelpController do
it "should have proper routes" do
{:get => "/halp/772/parser/18/edit" }.should be_routable
{:post => "/halp/772/parser" }.should be_routable
{:delete => "/halp/772/parser/18" }.should be_routable
end
end
Your file could look like this:
require 'spec_helper'
feature "Help" do
scenario "creation" do
visit help_path # access your help path
page.should have_content("Help") # verify content
end
end
If you need some help with testing with Capybara you could find some here:
Railscasts #257 Request Specs and Capybara or
Capybara repo at Github
Use have_tag matcher. Here is a cheatsheet.
response.should have_tag("a[href$=#{help_path}]", :text => "Help")
P.S. Did you read rails guides?
click_link "Help"
Gist of a capybara cheatsheet: https://gist.github.com/zhengjia/428105