Not sure what the issue here is as I have the same syntax in other tests. I have a table, and I'm checking that content in is in the table header. here's my test:
before(:each) do
#index = get :index, id: #user, user_id: #user.id
end
it "should be successful" do
#index
response.should be_success
end
it "should have the right title" do
#index
response.should have_selector('title', content: "All classes")
end
it "should have an element for each class" do
#index
#student_groups.each do |student_group|
response.should have_selector('th', content: student_group.name)
end
end
Here's the response.body:
<th>Class 2</th>
And here's the error autotest is throwing:
Failure/Error: response.should have_selector('th', content: student_group.name)
expected following output to contain a <th>class 2</th> tag
So why is this getting read so literally? the student_group.name IS inside of the tags...
The issue here should have been obvious:
The test was expecting:
<th>class 2</th>
And it was getting:
<th>Class 2</th>
So as Narfanator mentioned in the comments, the problem was that this is case sensitive. Woops!
Related
In my tickets_controller.rb:
def create
#ticket = #project.tickets.build(ticket_params)
#ticket.author = current_user
if #ticket.save
flash[:notice] = "Ticket has been created."
redirect_to [#project, #ticket]
else
flash.now[:alert] = "Ticket has not been created."
render "new"
end
end
So I assumed, I should be OK to pass the test, but it's giving me the errors below.
I'm under impression it's not invoking the email address from current_user.email...
The repo here https://github.com/tenzan/ticketee.
Deployed version here https://github.com/tenzan/ticketee
$ rspec spec/features/creating_tickets_spec.rb
...F
Failures:
1) Users can create new tickets with valid attributes
Failure/Error: expect(page).to have_content "Author: #{user.email}"
expected to find text "Author: test4#example.com" in "Internet Explorer Non-standards compliance My pages are ugly!"
# ./spec/features/creating_tickets_spec.rb:36:in `block (3 levels) in <top (required)>'
# ./spec/features/creating_tickets_spec.rb:35:in `block (2 levels) in <top (required)>'
Finished in 0.64558 seconds (files took 1.3 seconds to load)
4 examples, 1 failure
Failed examples:
rspec ./spec/features/creating_tickets_spec.rb:30 # Users can create new tickets with valid attributes
UPDATE 1:
show.html.erb for ticket:
<header>
<h2><%= #ticket.name %></h2>
<ul class="actions">
<li><%= link_to "Edit Ticket", [:edit, #project, #ticket],
class: "edit" %></li>
<li><%= link_to "Delete Ticket", [#project, #ticket], method: :delete,
data: { confirm: "Are you sure you want to delete this ticket?"},
class: "delete" %></li>
</ul>
</header>
<table id="attributes">
<tr>
<th>Author: </th>
<td><%= #ticket.author.email %></td>
</tr>
<tr>
<th>Created: </th>
<td><%= time_ago_in_words(#ticket.created_at) %> ago</td>
</tr>
</table>
<div id="ticket">
<header>
<h1><%= #project.name %></h1>
</header>
<header>
<h2><%= #ticket.name %></h2>
</header>
<%= simple_format(#ticket.description) %>
</div>
creating_tickets_specs.rb:
require 'rails_helper'
RSpec.feature 'Users can create new tickets' do
let(:user) { FactoryGirl.create(:user) }
before do
login_as(user)
project = FactoryGirl.create(:project, name: "Internet Explorer")
visit project_path(project)
click_link "New Ticket"
end
scenario "with valid attributes" do
fill_in "Name", with: "Non-standards compliance"
fill_in "Description", with: "My pages are ugly!"
click_button "Create Ticket"
expect(page).to have_content "Ticket has been created."
within("#ticket") do
expect(page).to have_content "Author: #{user.email}"
end
end
scenario "when providing invalid attributes" do
click_button "Create Ticket"
expect(page).to have_content "Ticket has not been created."
expect(page).to have_content "Name can't be blank"
expect(page).to have_content "Description can't be blank"
end
scenario "with an invalid description" do
fill_in "Name", with: "Non-standards compliance"
fill_in "Description", with: "It sucks"
click_button "Create Ticket"
expect(page).to have_content "Ticket has not been created."
expect(page).to have_content "Description is too short"
end
end
A couple things
After you set author on ticket, are you calling save?
What is in the show template where it's blowing up?
Typically I would write your controller code as:
current_user.tickets.create ticket_params
Which, assuming the relationships are set up correctly, will automatically set up the relationships as you would expect.
Rspec was looking for "Author: test4#example.com" within ticket tag as per within("#ticket") as described in the creating_tickets_spec.rb, but in my case it was out of that scope.
So, putting it inside solved the issue.
I've got a controller test set running where three of the tests succeed and three fail with the same type of error.
For the tests for the edit, update, and destroy actions, I get the associated error saying No route matches {:controller=>"accounts", action=>"edit"}
accounts_controller_spec.rb
describe AccountsController do
before(:each) do
#account_code = FactoryGirl.create(:account)
end
describe "GET 'index'" do
it "returns http success" do
get 'index'
expect(response).to be_success
end
end
describe "GET 'new'" do
it "returns http success" do
get 'new'
expect(response).to be_success
end
end
describe "POST 'create'" do
it "returns http success" do
post 'create'
expect(response).to be_success
end
end
describe "GET 'edit'" do
it "returns http success" do
get 'edit'
expect(response).to be_success
end
end
describe "POST 'update'" do
it "returns http success" do
post 'update'
expect(response).to be_success
end
end
describe "DELETE 'destroy'" do
it "returns http success" do
post 'destroy'
expect(response).to be_success
end
end
end
accounts_controller.rb
class AccountsController < ApplicationController
load_and_authorize_resource
def index
end
def new
end
def create
if #account.save
flash[:success] = "Account created"
redirect_to :action => :index
else
render 'new'
end
end
def update
if #account.update_attributes(params[:account])
flash[:success] = "Account Updated"
redirect_to :action => :index
else
render 'edit'
end
end
def edit
end
def destroy
#account.destroy
flash[:success] = "Account Deleted"
redirect_to accounts_path
end
end
routes.rb
resources :account_codes
I see two errors here
you do not use the correct verbs for destroy and update, you should use 'delete' for destroy and 'put' for update
you do not provide an 'id' for these actions, you should use get :edit, id: 1 , put :update, id: 1 ...
try running rake routes to see your exact routes
PS: I think you would get the same error for a show action as well. If you do not need that action, pass it in as except: :show in your resources on routes.rb
I am following the code and what the pages say specifically, and the only thing I'm missing is the rspec gem for Ruby on Rails, as I was unable to get it (gives this error for rspec installation: "E: Unable to locate package rspec" so any help with that would be greatly appreciated) due to the inability to locate the package.
This is my entire pages_controller_spec.rb file, and the error displayed when the rails server tried to connect to the page is displayed in the title (if it's unable to be seen here it is again: "undefined method `describe' for PagesController:Class").
Note: I have also tried the code without "require 'spec_helper'" and it still will not operate.
class PagesController < ApplicationController
def home
end
def contact
end
def about
end
require 'spec_helper'
describe PagesController do
render_views
describe "GET 'home'" do
it "should be successful" do
get 'home'
response.should be_success
end
it "should have the right title" do
get 'home'
response.should have_selector("title",
:content => "Ruby on Rails Tutorial Sample App | Home")
end
end
describe "GET 'contact'" do
it "should be successful" do
get 'contact'
response.should be_success
end
it "should have the right title" do
get 'contact'
response.should have_selector("title",
:content => "Ruby on Rails Tutorial Sample App | Contact")
end
end
describe "GET 'about'" do
it "should be successful" do
get 'about'
response.should be_success
end
it "should have the right title" do
get 'about'
response.should have_selector("title",
:content => "Ruby on Rails Tutorial Sample App | About")
end
end
end
end
you need additional end before spec helper require simply you are in the controller class and he is trying to call describe as method on controller. add it and it will be fine.
so it should be like this:
class PagesController < ApplicationController
def home
end
def contact
end
def about
end
end
and rest of file.
Am working my way through Michael Hartl's Rails Tutorial and am now in Chapter 5. However, I cannot get my Rspec, spark and auto tests to run. When I try to get them to run, I get an error message:
Paul-Denlingers-MacBook-Pro:sample_app_2 pdenlinger$ rspec spec/
No DRb server is running. Running in local process instead ...
/Users/pdenlinger/.rvm/gems/ruby-1.9.2-p0#rails3tutorial/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:419:in `load': /Users/pdenlinger/rails_projects/sample_app_2/spec/controllers/pages_controller_spec.rb:14: syntax error, unexpected keyword_end, expecting $end (SyntaxError)
from /Users/pdenlinger/.rvm/gems/ruby-1.9.2-p0#rails3tutorial/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:419:in `block in load_spec_files'
from /Users/pdenlinger/.rvm/gems/ruby-1.9.2-p0#rails3tutorial/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:419:in `map'
from /Users/pdenlinger/.rvm/gems/ruby-1.9.2-p0#rails3tutorial/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:419:in `load_spec_files'
from /Users/pdenlinger/.rvm/gems/ruby-1.9.2-p0#rails3tutorial/gems/rspec-core-2.6.4/lib/rspec/core/command_line.rb:18:in `run'
from /Users/pdenlinger/.rvm/gems/ruby-1.9.2-p0#rails3tutorial/gems/rspec-core-2.6.4/lib/rspec/core/runner.rb:80:in `run_in_process'
from /Users/pdenlinger/.rvm/gems/ruby-1.9.2-p0#rails3tutorial/gems/rspec-core-2.6.4/lib/rspec/core/runner.rb:66:in `rescue in run'
from /Users/pdenlinger/.rvm/gems/ruby-1.9.2-p0#rails3tutorial/gems/rspec-core-2.6.4/lib/rspec/core/runner.rb:62:in `run'
from /Users/pdenlinger/.rvm/gems/ruby-1.9.2-p0#rails3tutorial/gems/rspec-core-2.6.4/lib/rspec/core/runner.rb:11:in `block in autorun'
What does this mean, and how can I fix it? Thanks in advance!
pages_controller_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 a 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
All of your do keywords are enclosed in your example descriptions. Change:
it "should have a Home page at '/' do"
to:
it "should have a Home page at '/'" do
and repeat for each of your examples.
If you read the error message, it looks like you have a missing end in this file:
spec/controllers/pages_controller_spec.rb
at line 14.
Update after #pdenlinger fixed his post:
You are enclosing the do keyword in quotes when you shouldn't. It should look like this instead:
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 a 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
I have been having this issue for a while now and I cannot seem to wrap my brain around as I have recreated everything from scratch numerous times
Anyways I am at the part of the book where we want the home page link to go from http://localhost:3000/pages/home (This worked perfectly by the way) to http://localhost:3000
(basically meaning I want the home page to show up on the root page)
Currently the error i have is
LayoutLinks should have a Home page at '/'
Failure/Error: response.should have_selector('title', :content => "Home")
And underneath it is a bunch of text saying waht the page should look like.
Now I did 2 things, I first created the layout_links_spec.rb page, added the help page to the pages_controller.rb, and added the routes to config/routes.rb below is the code for all 3
layout_links_spec.rb
require 'spec_helper'
describe "LayoutLinks" do
# describe "GET /layout_links" do
# it "works! (now write some real specs)" do
# Run the generator again with the --webrat flag if you want to use webrat methods/matchers
# get layout_links_index_path
# response.status.should be(200)
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 '/about'
response.should have_selector('title', :content => "About")
end
it "should have a Help page '/help'" do
get '/help'
response.should have_selector('title', :content => "Help")
end
end
Pages_controller.rb
def home
#title = "Home"
end
def contact
#title = "Contact"
end
def about
#title = "About"
end
def help
#title = "Help"
end
end
config/routes.rb
SampleApp::Application.routes.draw do
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
root :to => 'pages#home'
end
Is there something I am doing wrong?
Add "end" to the end of your routes.rb :)
There is a block, that opens with "do" and you didn't close it. Check the listing 5.20 carefully.