Action view template error when running cucumber - ruby-on-rails-3

Here is a cucumber scenario:
Scenario: Login
Given the user is on Login page
When I sign in
My step definition:
When /I sign in/ do
within("#mypage") do
fill_in 'login_id', :with => 'ccd'
fill_in 'password', :with => 'coffee'
end
click_button 'Login'
visit '/feedbacks/'
end
When I run cucumber, I get the following error:
Scenario: Login # features\heartbeat_tests.feature:20
Given the user is on Login page # features/step_definitions/heartbeat_definitions.rb:5
When I sign in # features/step_definitions/heartbeat_definitions.rb:18
undefined method `each' for nil:NilClass (ActionView::Template::Error)
D:/shoutout_web/app/views/layouts/application.html.erb:256:in
`_app_views_layouts_application_html_erb__124036717_28160832'

Related

Unable to use Capybara, Not sure where I am going wrong 'background' and 'it' etc is not working

Hi I am learning capybara, and i ve tried various answers in SO I am not able to proceed. I am trying to test a application with capybara. Visit is not working, errors out saying it needs to be within 'it' or example/executable block etc. but with below code , background and scenario etc is not working, execution is not going inside.
Results::
C:\Ruby223\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) C:/Users/xxx/RubymineProjects/xxx-app/spec/features/xxxxx.rb
"aaa"
Process finished with exit code 0
i dont want to mess up the Rails application, so I want a standalone file from where i can execute tests and then integrate with rails app with developers help.
require 'capybara/dsl'
require 'capybara'
require 'capybara/rspec'
require 'rspec'
feature "Signing in" do
p 'aaa'
background do
p 'aaa1'
Capybara.register_driver :selenium_chrome do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
Capybara.run_server = false
Capybara.current_driver = :selenium_chrome
Capybara.app_host = 'http://www.google.com'
end
scenario "Signing in with correct credentials" do
p 'aaa2'
visit '/q=?alkjaaa'
within("#session") do
fill_in 'email', with: 'user#example.com'
fill_in 'password', with: 'caplin'
end
click_button 'Signin'
expect(page).to have_content 'results'
end
given(:other_user) { User.make(email: 'other#example.com', password: 'rous') }
scenario "Signing in as another user" do
p 'aaa'
visit '/sessions/new'
within("#session") do
fill_in 'Email', with: other_user.email
fill_in 'Password', with: other_user.password
end
click_button 'Signin'
expect(page).to have_content 'Invalid email or password'
end
end
Running that file alone with ruby isn't going to do anything because all it does is define tests, it doesn't execute them. If you run that file with rspec then it will run the tests defined in the file.
rspec your_file.rb

active_admin login page testing with Capybara

visit new_admin_user_session_path
fill_in('admin_user_email', :with => "admin#example.com")
fill_in('admin_user_password', :with => "password")
click_button('Login')
page.must_have_content("Signed in successfully")
After running my test i am getting error like :
MiniTest::Assertion: expected there to be text "Signed in successfully" in "Invalid email or password. Shakti Login Email*Password*Remember me Forgot your password?".
.rvm/gems/ruby-1.9.3-p362/gems/capybara_minitest_spec-1.0.0/lib/capybara_minitest_spec.rb:42:in `block in define_assertion'
(eval):6:in `must_have_content'
Most likely the user is not in the db. Create it first:
admin = FactoryGirl.create(:admin_user, email: 'admin#example.com', password: 'password') if you are using FactoryGirl

why controller var isn't available within a test?

I'm trying to follow Michael Hartl's Ruby on Rails Tutorial in http://ruby.railstutorial.org/chapters/sign-in-sign-out, but with some changes to practice, above all, some variations and the Test::Unit framework. In the tutorial, RSpec is used, while I'm trying to stick to Test::Unit + Shoulda-context.
In chapter 9 I'm suposed to pass some functional tests that use a var called 'controller', but my tests don't work as they find out that 'controller' doesn't exist. This is what I get:
marcel#pua:~/Desenvolupament/Rails3Examples/ror_tutorial$ rake
test:recent Loaded suite
/home/marcel/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/rake_test_loader
Started F
=============================================================================== Failure: test: POST 'create' with valid signin (email and password)
should redirect to the user show page. (SessionsControllerTest)
[test/functional/sessions_controller_test.rb:58]: Expected at least 1
element matching "title", found 0. is not true.
=============================================================================== E
=============================================================================== Error: test: POST 'create' with valid signin (email and password)
should sign in the user. (SessionsControllerTest): NameError:
undefined local variable or method `controller' for
test/functional/sessions_controller_test.rb:53:in `block (3 levels) in <class:SessionsControllerTest>'
=============================================================================== Finished in 0.957865676 seconds. 7 tests, 6 assertions, 1 failures, 1
errors, 0 pendings, 0 omissions, 0 notifications 0% passed
7.31 tests/s, 6.26 assertions/s rake aborted! Command failed with status (1): [/home/marcel/.rvm/rubies/ruby-1.9.2-p290/b...] Tasks: TOP
=> test:recent (See full trace by running task with --trace)
This is the original (RSpec) test:
describe SessionsController do
...
describe "POST 'create'" do
...
describe "with valid email and password" do
before(:each) do
#user = Factory(:user)
#attr = { :email => #user.email, :password => #user.password }
end
it "should sign the user in" do
post :create, :session => #attr
controller.current_user.should == #user
controller.should be_signed_in
end
it "should redirect to the user show page" do
post :create, :session => #attr
response.should redirect_to(user_path(#user))
end
end
end
end
and this is my translated (into Test::Unit + Sholuda-context) test:
class SessionsControllerTest < ActionController::TestCase
context "POST 'create'" do
context "with valid signin (email and password)" do
setup do
#attr = {email: "test#email.tst", password: "testpwd"}
#user=User.create! #attr.merge!({name: "test_user", password_confirmation: "testpwd"})
end
should "sign in the user" do
post :create, :session => #attr
assert_equal #user, controller.current_user
end
should "redirect to the user show page" do
post :create, :session => #attr
assert_select "title", /Show/
end
end
end
end
Has anybody any idea how to make my test work?
Looking at the official Rails testing guide at http://guides.rubyonrails.org/testing.html, I've seen that an instance variable called #controller is enabled in functional tests. so, the Test::Unit version should be:
should "sign in the user" do
post :create, :session => #attr
assert_equal #user, #controller.current_user
end

undefined method `change'

I'm doing the ruby on rails tutorial, I specifically adding listing 8.21 to spec/requests/users_spec.rb
require 'spec_helper'
describe "Users" do
describe "signup" do
describe "signup failure" do
lambda do
it "should not make a new user" do
visit signup_path
fill_in "Name", :with => ""
fill_in "Email", :with => ""
fill_in "Password", :with => ""
fill_in "Password confirmation", :with => ""
click_button
response.should render_template('users/new')
response.should have_selector("div#error_explanation")
end
end.should_not change(User, :count)
end
end
end
as far as I can tell, this is exactly like the listing in 8.21; however, when I run
rspec spec/requests/users_spec.rb -e "Users"
I got the following error ...
#> rspec spec/requests/users_spec.rb -e "Users" No DRb server is running. Running in local process instead ... /Users/bryanjamieson/rails_projects/sample_app/spec/requests/users_spec.rb:17:in `block (3 levels) in ': undefined method `change' for
# (NoMethodError)
any help would be appreciated.
I'd use User.count
end.should_not change(User.count)
Depending on which rails version and rspec version you're using (assuming Rails 3.1, Rspec2) the tutorial may be a little of out date

Capybara-Selenium RSpec test hangs for a long time before continuing

I'm running some request specs using the selenium driver for capybara, and while they work, they take too long to complete. The test starts, Firefox starts, clicks on the link and hangs for like 15 seconds before filling in the form. Here's the spec code:
it "should let an invited user sign up" do
# Invite the user
invitation = Factory.create :invitation, :invitee => nil
# Sign up
visit invitation_path(:controller => :sessions, :action => :invitation, :token => invitation.token)
current_path.should eq(login_path)
click_link "Connect with Facebook"
current_path.should eq(edit_user_path(User.order('created_at desc').first))
# Fill form
# THIS IS THE PART THAT TAKES WAY TOO LONG TO BEGIN
fill_in "Username", :with => "johndoe"
fill_in "Email", :with => "john#doe.com"
fill_in "City", :with => "Santiago"
fill_in "Commune", :with => "Santiago"
select_date Time.now, :from => "user_birthday"
click_on "Save"
# THIS ASSERTION ALSO TAKES VERY LONG TO BE RUN
current_path.should eq(root_path)
end
Any ideas as to what is going on? Is this expected behavior for selenium? Is there a way to speed this up?
From Original Poster:
I found out it had to do with some long-polling javascript on the page, so selenium didn't detect that the page had finished loading (because of the long-poll request on the background).