I am following the Ruby on Rails Tutorial for Rails 3, located here:
http://ruby.railstutorial.org
I am at this section of the tutorial:
http://ruby.railstutorial.org/chapters/filling-in-the-layout#sec:integration_tests
All of my pages render as expected at this point; e.g., I can hit http://localhost:3000/about and see the about page of the sample application, with the expected title ("Ruby on Rails Tutorial Sample App | About"). Yet the integration tests from this section keep failing.
Here is my layout_links_spec.rb:
require 'spec_helper'
describe "LayoutLinks" do
it "should have a Home page at '/'" do
get '/'
response should have_selector('title', :content => "Home")
end
it "should have a Contact page at '/contact'" do
get '/contact'
response should have_selector('title', :content => "Contact")
end
it "should have an About page at '/about'" do
get '/about'
response should have_selector('title', :content => "About")
end
it "should have a Help page at '/help'" do
get '/help'
response should have_selector('title', :content => "Help")
end
end
When I run tests with rspec spec/ or via autotest, I get the following test failures:
........FFFF
Failures:
1) LayoutLinks should have a Home page at '/'
Failure/Error: response should have_selector('title', :content => "Home")
expected following output to contain a <title>Home</title> tag:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p>LayoutLinks</p></body></html>
# ./spec/requests/layout_links_spec.rb:7:in `block (2 levels) in <top (required)>'
2) LayoutLinks should have a Contact page at '/contact'
Failure/Error: response should have_selector('title', :content => "Contact")
expected following output to contain a <title>Contact</title> tag:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p>LayoutLinks</p></body></html>
# ./spec/requests/layout_links_spec.rb:12:in `block (2 levels) in <top (required)>'
3) LayoutLinks should have an About page at '/about'
Failure/Error: response should have_selector('title', :content => "About")
expected following output to contain a <title>About</title> tag:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p>LayoutLinks</p></body></html>
# ./spec/requests/layout_links_spec.rb:17:in `block (2 levels) in <top (required)>'
4) LayoutLinks should have a Help page at '/help'
Failure/Error: response should have_selector('title', :content => "Help")
expected following output to contain a <title>Help</title> tag:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p>LayoutLinks</p></body></html>
# ./spec/requests/layout_links_spec.rb:22:in `block (2 levels) in <top (required)>'
Finished in 0.23497 seconds
12 examples, 4 failures
Failed examples:
rspec ./spec/requests/layout_links_spec.rb:5 # LayoutLinks should have a Home page at '/'
rspec ./spec/requests/layout_links_spec.rb:10 # LayoutLinks should have a Contact page at '/contact'
rspec ./spec/requests/layout_links_spec.rb:15 # LayoutLinks should have an About page at '/about'
rspec ./spec/requests/layout_links_spec.rb:20 # LayoutLinks should have a Help page at '/help'
...note that the response shown in the test failures appears to indicate that I'm getting a mock/stubbed response. I believe that this is my issue, but I don't know why it is happening.
Finally, here is my environment:
Gatito:sample_app abrown$ gem list
*** LOCAL GEMS ***
abstract (1.0.0)
actionmailer (3.0.9)
actionpack (3.0.9)
activemodel (3.0.9)
activerecord (3.0.9)
activeresource (3.0.9)
activesupport (3.0.9)
arel (2.0.10)
autotest (4.4.6)
autotest-fsevent (0.2.4)
autotest-growl (0.2.9)
autotest-rails-pure (4.1.2)
builder (2.1.2)
bundler (1.0.15)
configuration (1.2.0)
diff-lcs (1.1.2)
erubis (2.6.6)
heroku (2.3.3)
i18n (0.5.0)
launchy (0.4.0)
mail (2.2.19)
mime-types (1.16)
nokogiri (1.4.6)
polyglot (0.3.1)
rack (1.2.3)
rack-mount (0.6.14)
rack-test (0.5.7)
rails (3.0.9)
railties (3.0.9)
rake (0.9.2, 0.8.7)
rdoc (3.6.1)
rest-client (1.6.3)
rspec (2.6.0)
rspec-core (2.6.4)
rspec-expectations (2.6.0)
rspec-mocks (2.6.0)
rspec-rails (2.6.1)
sequel (3.20.0)
sinatra (1.0)
spork (0.9.0.rc8)
sqlite3 (1.3.3)
sqlite3-ruby (1.3.3)
sys-uname (0.8.5)
taps (0.3.23)
term-ansicolor (1.0.5)
thor (0.14.6)
treetop (1.4.9)
tzinfo (0.3.28)
webrat (0.7.1)
ZenTest (4.5.0)
I figured it out. I typed in the tests above myself. When I re-copied/pasted the tests directly from the tutorial, they began passing. I was mystified, as everything looked identical at a glance. But after running a diff, I realized that for each test, I had typed:
response should
...but that I should have typed:
# Note the "."
response.should
After making this correction to each test, they all pass as expected.
I've experienced the exact same error, but the cause was forgetting to include render_views line in the test:
require 'spec_helper'
describe PagesController do
render_views
describe "GET 'home'" do
it "should be successful" do
get 'home'
response.should be_success
end
end
Related
I'm making RSpec test.
Our website like this. It's plain website.
https://somewebsite.com/restaurant
So every time I try to test, it shows this kind of error.
$ bundle exec rspec spec/requests/restaurant_spec.rb
Restaurants
GET /index
index responds successfully (FAILED - 1)
Failures:
1) Restaurants GET /index index responds successfully
Failure/Error: get :index
URI::InvalidURIError:
bad URI(is not URI?): http://www.example.com:80index
# ./spec/requests/restaurant_spec.rb:6:in `block (3 levels) in <top (required)>'
Finished in 0.00484 seconds (files took 6.41 seconds to load)
1 example, 1 failure
My test code is this.
require 'rails_helper'
RSpec.describe "Restaurants", type: :request do
describe "GET /index" do
it "index responds successfully" do
get :index
expect(response).to be_success
end
end
end
And my application code here.
def index
#q = Restaurant.ransack(params[:q])
if params[:q].present?
#all_restaurants = #q.result(distinct: true).page(params[:page])
else
#all_restaurants = Restaurant.page(params[:page])
end
end
BTW my routing is this.
scope "(:locale)", locale: /en|ja/ do
get 'restaurant/' => 'restaurant#index'
get 'restaurant/:id' => 'restaurant#show'
end
Why this error happens? Please let me know. Thanks in advance.
Appendix
$ ruby -v
ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-darwin17]
$ rails -v
Rails 5.2.2
RSpec
rspec-core (3.10.1)
rspec-support (~> 3.10.0)
rspec-expectations (3.10.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-mocks (3.10.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-rails (5.0.1)
I think this error is related to the describe block in RSpec. Change "Restaurants" to just Restaurant (singular and without the quotation marks).
require 'rails_helper'
RSpec.describe Restaurant, type: :request do
describe "GET /index" do
it "index responds successfully" do
get :index
expect(response).to be_success
end
end
end
Also, I think you'll have to change your routes to get this to work properly. Since you have a resourceful controller, use resource routing:
scope "(:locale)", locale: /en|ja/ do
resources :restaurants, only: %i[index show]
end
Should give you the routes /restaurants and /restaurants/:id with your desired locales, which is the Rails convention for this type of routing. And it'll also allow you to use :index and :show in your RSpec tests.
I dont think this issue CanCan uninitialized constant Ability::CanCan addresses my problem.
According to CanCan Its not hard to test the Abilities . I followed https://github.com/ryanb/cancan/wiki/Testing-Abilities to write specs:
When I try the following command
bundle exec rspec spec/cancan/ability.rb
I get the following error
/projects/ATS/app/models/ability.rb:2:in `<class:Ability>': uninitialized constant Ability::CanCan (NameError)
from /home/shiva/projects/ATS/app/models/ability.rb:1:in `<top (required)>'
from /home/shiva/projects/ATS/spec/cancan/ability.rb:3:in `<top (required)>'
from /home/shiva/.rvm/gems/ruby-2.1.1/gems/cancan-1.6.10/lib/cancan.rb:1:in `<top (required)>'
from /home/shiva/projects/ATS/spec/rails_helper.rb:14:in `<top (required)>'
from /home/shiva/projects/ATS/spec/cancan/ability.rb:1:in `<top (required)>'
My code in spec/cancan/ability.rb
require 'rails_helper'
RSpec.describe Ability, type: :model do
subject(:ability) {Ability.new(user)}
let(:user) {nil}
context 'Logged in as CSA' do
let(:org) {create(:organization)}
let(:user) {create(:user, organization: org, role_ids: [Role::ROLE_CSA])}
describe 'Not Permitted' do
it 'to visit /admins' do
it {is_expected.to be_able_to(:manage, Workflow.new)}
end
end
end
end
and code in snippet in models/ability.rb
class Ability
include CanCan::Ability
def initialize(user, url, admin_session_id, my_applicant_id)
if user.present?
user.roles[0].permissions_by_organization(user).each do |permission|
case permission.subject_class
Version Details of components
Rails 4.0.4
rspec-core (3.1.7)
rspec-expectations (3.1.2)
rspec-mocks (3.1.3)
rspec-rails (3.1.0)
rspec-support (3.1.2)
cancan (1.6.10)
I got the answer for my question.
The issue was the filename of the spec file
It should have been
spec/cancan/ability_spec.rb but it was
spec/cancan/ability.rb
and I should have invoked
bundle exec rspec spec/cancan/ability_spec.rb
For more details please visit this link RSpec naming conventions for files and directory structure
I am working on migrating the test suite for my project to rspec. I am getting the following error trying to access my fixtures:
undefined method `[]' for nil:NilClass
Here is an example spec file:
require 'rails_helper'
feature "edit contacts" do
fixtures :contacts
before(:all) do
#thing = contacts(:two)
end
scenario "do a thing" do
visit new_contact_path
end
end
I get the error in the before block, on the call to contacts(:two)
Here is config info from rails_helper.rb
RSpec.configure do |config|
config.infer_spec_type_from_file_location!
config.fixture_path = "#{::Rails.root}/spec/fixtures"
end
Here is version info from Gemfile.lock:
rails (4.0.13)
rspec-core (3.2.2)
rspec-expectations (3.2.0)
rspec-mocks (3.2.1)
rspec-rails (3.2.1)
rspec-support (3.2.2)
I'm happy to provide any additional supporting information if needed. Thanks for taking a look, any help appreciated!
ok - finally resolved this with the help of a coworker. The issue was that I should have been using before(:each) instead of before(:all).
Apparently, using :each triggers a 'before_setup' callback in ActiveRecord::FixtureSet which sets up all the fixtures, and :all doesn't.
According to the docs:
before(:each) blocks are run before each example before(:all) blocks are run once before all of the examples in a group
I'm doing the rails tutorial from Michael Hartl, and I can't get the test in chapter 3 to pass.
The static_pages.rb file:
require 'spec_helper'
describe "Static pages" do
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/static_pages/home'
expect(page).to have_content('Sample App')
end
end
end
The home.html.erb file:
<h1>Sample App</h1>
<p>
This is the home page for the
Ruby on Rails Tutorial
sample application.
</p>
And this is the result of the test:
An error occurred in an after hook
ArgumentError: rack-test requires a rack application, but none was given
occurred at C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/capybara-2.1.
0/lib/capybara/rack_test/driver.rb:16:in `initialize'
←[31mF←[0m
Failures:
1) Static pages Home page should have the content 'Sample App'
←[31mFailure/Error:←[0m ←[31mvisit '/static_pages/home'←[0m
←[31mArgumentError←[0m:
←[31mrack-test requires a rack application, but none was given←[0m
←[36m # ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in <top
(required)>'←[0m
Finished in 0.001 seconds
←[31m1 example, 1 failure←[0m
Failed examples:
←[31mrspec ./spec/requests/static_pages_spec.rb:7←[0m ←[36m# Static pages Home p
age should have the content 'Sample App'←[0m
My current version of Capybara is 2.1.0 but I tried changing version to 1.1.2 and bundle update, but didn't work aswell.
Also tried different syntax for the test.
page.should have_title("Sample App")
page.should have_selector('h1', :text => 'Sample App')
page.should have_selector('title', text: 'Sample App')
My system is Windows 7, and my rails version is 4.0.8 and Ruby version is 1.9.3.
Tried searching around for the "rack-test requires a rack application, but none was given" error, and it seems I somehow need to change so that rack is not the default driver.
Well this is how my config file looks like.
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'capybara/rspec'
RSpec.configure do |config|
config.include Capybara::DSL
end
I am running BDD steps with cucumber to implement my autlogic login behavior.
Scenario: log in
Given a registered user: "test#test.com" with password: "p#ssword" exists
And I am on the homepage
When I follow "Log in"
And I fill in "Username" with "test#test.com"
And I fill in "Password" with "p#ssword"
And I open the page
And I press "Login"
And I open the page
Then I should see "Login successful!"
And I should see "Logout"
this is my scenario and when I hit
Then I should see "Login successful!"
my cucumber step fails but only in webrat. I debugged with launchy and when the web-page appeard there is indeed no message but in development mode, when I run script/server I see my messages.
My controller looks like this
def create
#user_session = UserSession.new(params[:user_session])
if #user_session.save
flash[:notice] = "Login successful!"
redirect_to root_url
else
render :action => 'new'
end
end
And yes I saw this
http://github.com/binarylogic/authlogic/issuesearch?state=open&q=cucumber+flash#issue/142
and did not understand how this could help me
and this also did not help
Rails/Cucumber/Webrat: redirect_to, flash[:notice] not working
my setup is
*** LOCAL GEMS ***
actionmailer (2.3.8)
actionpack (2.3.8)
activerecord (2.3.8)
activeresource (2.3.8)
activesupport (2.3.8)
authlogic (2.1.5)
builder (2.1.2)
configuration (1.1.0)
cucumber (0.8.4, 0.8.3)
cucumber-rails (0.3.2)
database_cleaner (0.5.2)
declarative_authorization (0.4.1)
diff-lcs (1.1.2)
gherkin (2.1.2, 2.0.2)
json_pure (1.4.3)
launchy (0.3.5)
mysql (2.8.1)
nokogiri (1.4.2)
paperclip (2.3.3)
pickle (0.3.0)
rack (1.2.1, 1.1.0)
rack-test (0.5.4)
rails (2.3.8)
rake (0.8.7)
rspec (1.3.0)
rspec-rails (1.3.2)
syntax (1.0.0)
term-ansicolor (1.0.5)
thoughtbot-factory_girl (1.2.2)
trollop (1.16.2)
webrat (0.7.1)
ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]
gem 1.3.7
running on Ubuntu
What can I do to make my cucumber steps pass!
Thanks for any help
instead of the line
flash[:notice] = "Login successful!"
redirect_to root_url
just try rendering it instead of redirecting it.
flash[:notice] = "Login successful!"
render :action => :root_url
in doing this, it seems to remember flash[:notice]
i could not get --> redirect_to root_url, :flash => { :notice => 'not found' }, :notice => Login successful!' <-- to work at all
Looks like rails 2.3.8 has changed the method in which is displays notices..
redirect_to(root_url, :notice => 'Login successful!')
might be what you're looking for.
I believe this issue will be fixed in Rails 2.3.9. It has to do with setting both a cookie and a session in the same request. See this ticket for details.
In the meantime you can use this gist as a temporary fix.
thanks to raynb who pointed me into the right direction. But unfurtunatly this solution did not work but I found this page which puts everything in cucumber.rb... which mad it running now ...
https://webrat.lighthouseapp.com/projects/10503-webrat/tickets/383-reset_session-and-webrat-dont-play-nicely-with-one-another