Rails webrat gem - render method missing - ruby-on-rails

I am using rspec and webrat for behavior driven development. This is the first time I am using webrat.
I followed webrat's instructions on how to use the gem, so I think it is installed correctly (the gem shows when I use bundle show).
The problem occurs when I just want to test whether my rendered view (welcome/index.html.erb) has an h1 tag. Should be fairly simple.
My code looks like this:
require 'spec_helper'
describe "welcome/index.html.erb" do
render :template => "welcome/index", :layout => "layouts/application"
it "displays a welcome message" do
rendered.should have_selector "h1"
end
end
I got there because of this question, but maybe it is a little old. When I run the test it gives me
undefined method `render'
What am I doing wrong? How can I fix this?

Found a way to solve it. Apparently this is much easier than I thought:
describe "welcome/index.html.erb" do
it "displays a welcome message" do
render
rendered.should have_selector "h1"
end
end
Apparently I just have to say render inside the "it" block. This solves the problem for me.

Related

Unable to find css best_in_place with test helpers

I am trying to get best in place's
test helpers
working in my app. I am using
Capybara with Test::Unit
I have written a test like this.
class CompaniesTest < ActionDispatch::IntegrationTest
include BestInPlace::TestHelpers
def setup
#company = companies(:public)
end
test "should have best in place fields on show" do
get company_path(#company)
assert_response :success
assert_match #company.name, response.body
bip_text( #company, :name, "new name" )
end
end
I am getting an error like this
Capybara::ElementNotFound: Unable to find css "#best_in_place_company_1001664029_name"
I tried adding a sleep(0.5) before bip_text to see if it is a timing issue this did not change the error.
EDIT:
<span data-bip-type="input"
data-bip-attribute="name"
data-bip-object="company"
data-bip-original-content="Lightbulbs Corp."
data-bip-skip-blur="false"
data-bip-url="/companies/1001664029"
data-bip-value="Lightbulbs Corp."
class="best_in_place"
id="best_in_place_company_1001664029_name">Lightbulbs Corp.</span>
Here is what the css element for the page in the test env looks like. It looks like the id is correct. Any help would be appreciated.
I just reread your question and noticed you're using get. That's not a Capybara method and you can't mix the two different APIs and expect things to work. To implement what you're doing using Capybara it would be something like
test "should have best in place fields on show" do
visit company_path(#company)
assert_text #company.name
bip_text( #company, :name, "new name" )
end

Has anyone ever tested best_in_place test helper methods with RSpec?

I found this in one demo app:
it "should be able to use bip_text to update a text field" do
#user.save!
visit user_path(#user)
within("#email") do
page.should have_content("lucianapoli#gmail.com")
end
bip_text #user, :email, "new#email.com"
visit user_path(#user)
within("#email") do
page.should have_content("new#email.com")
end
end
https://github.com/dougc84/best_in_place/blob/master/spec/integration/js_spec.rb
Seems easy.
So I copied it to my Capybara spec:
before (:each) do
#report = FactoryGirl.create(:report)
visit report_path(#report)
end
it "name", :focus do
within("#name") do
page.should have_content #report.name
end
bip_text #report, :name, "new name"
visit report_path(#report)
within("#name") do
page.should have_content "new name"
end
end
It's so fast I can barely see anything, but it looks like it does do something with #name field. Then page reloads and it's still old value.
Any help?
Oh btw it does work in the browser. Just can't get tests pass.
I've added sleep 1 before and after bip_ helper, and it worked.
The problem here is the Javascript run by bip_text is asynchronous, but in your next line you immediately go to a different page, cutting off that Javascript from ever finishing. This is why your sleep 1 fixes it. You could also fix it by making Capybara wait for some new content before visit report_path, but then you'd need to write something like a success message to the page (e.g. with the ajax:success JS callback).

capybara rspec matcher equivalent of response.should have_selector "form", :action => some_path

Is there a matcher in capybara that will check for the presence of a form with a particular action? I'd like to be able to say something like this:
page.should have_form :action => some_action
It looks like the rspec-html-matchers gem does what I need, and more, but I find it strange that Capybara rspec matchers wouldn't provide something like it. Am I missing something?
You could use the xpath node matcher:
has_xpath?("//form[#action='/some_action']")
http://rubydoc.info/github/jnicklas/capybara/Capybara/Node/Matchers#has_xpath%3F-instance_method
Or you can use the regular matchers e.g. have_css()
have_css("form[action='/users/new']")

How to get Capybara matchers to work?

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.

Trouble on testing a JavaScript redirection

I am using Ruby on Rails 3.1.0 and the rspec-rails 2 gem. I would like to test a JavaScript redirection but I am some trouble on doing that.
In my controller file I have:
respond_to do |format|
format.js { render :js => "window.location.replace('#{users_url}');" }
end
In my spec file I have:
it "should redirect" do
xhr :post, :create
response.should redirect_to(users_url)
end
If I run the spec I get the following:
Failure/Error: response.should redirect_to(users_url)
Expected response to be a <:redirect>, but was <200>
How can I implement the spec code so to correctly test the JavaScript redirection?
ATTEMPTS AT SOLUTION
If I use
response.should render_template("window.location.replace('#{users_url}');")
I get
Failure/Error: response.should render_template("window.location.replace('#{users_url}');")
expecting <"window.location.replace('http://<my_application_url>/users');"> but rendering with <"">
If I use
response.should render_template(:js => "window.location.replace('#{users_url}');")
the spec successfully passes, but too much! That is, I can state also the following and the spec will (unexpectedly) pass:
response.should render_template(:js => "every_thing")
I have also seen this question\answer and the proposed solution works, but I think that is the best solution to accomplish what I aim.
RSpec controller specs use the rack_test driver, which does not execute Javascript. To test pages that use Javascript for redirection, you should really look at another driver.
Capybara with selenium-webdriver does this nicely. RSpec request specs replace your controller specs for those tests that rely on Javascript.

Resources