I've spent the last hour trying to figure out why this test won't pass.
I'm working in Rails 4.0 with Capybara 2.1.0 and rspec 2.14.1
Here's my test:
require 'spec_helper'
describe "Stats" do
subject { page }
describe "Index Page" do
before { visit stats_path }
it { should have_selector 'h1', text: 'Stats' }
end
end
And my index.html.erb file:
<h1>Stats</h1>
<table>
<thead>
<tr>
<th>Attendance</th>
<th>Salvations</th>
<th>Visitors</th>
<th>Offering</th>
<th>Service Date</th>
<th>Time</th>
<th>Campus</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% #stats.each do |stat| %>
<tr>
<td><%= stat.attendance %></td>
<td><%= stat.salvations %></td>
<td><%= stat.visitors %></td>
<td><%= stat.offering %></td>
<td><%= stat.date %></td>
<td><%= stat.time %></td>
<td><%= stat.campus.name %></td>
<td><%= link_to 'Show', stat %></td>
<td><%= link_to 'Edit', edit_stat_path(stat) %></td>
<td><%= link_to 'Destroy', stat, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Stat', new_stat_path %>
And the error I'm getting is:
1) Stats Index Page should have css "h1" with text "Stats"
Failure/Error: it { should have_selector 'h1', text: 'Stats' }
Capybara::ExpectationNotMet:
expected to find css "h1" with text "Stats" but there were no matches
# ./spec/features/stats_spec.rb:7:in `block (3 levels) in '
I'm stumped. Any help would be appreciated.
Incidentally, my index page renders correctly without any errors.
If it works when you visit then page but not when you run your test script then there is certainly a session or authentication matter. Is your index page protected by any authentication or authorization logic?
I cannot give you a solution, but I can try to give you a guide line, if you haven't do it yet, use pp to output to the console the rendering of the page.
For example, if you are testing in controllers directory, use pp response.body. Or if you are testing in views directory, use render followed by pp rendered.
Then from the output in your console you will certainly get a clue according to your specific logic on what's append.
Related
Can anyone tell me why the email addresses are not showing in the users list view below?
<p id="notice"><%= notice %></p>
<h1>Users</h1>
<table>
<thead>
<tr>
<th>Email</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #users.each do |user| %>
<tr>
**<td><%= user.email %></td>**
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New User', new_user_path %>
I'm not familiar with seeing the two asterisks around the element. I'm thinking that might be messing something up. If you want to bold the user's email you could try something like <td><b><%= user.email %></b></td>.
Are you getting any errors anywhere?
If the asterisks are just there to help us see the line of code, and you say that you see the users (and their emails) in the db, I think it could be one of two other relatively minor things.
Whats your controller look like for this view? Are you passing #users to this view? You could try just dumping #users on the page to make sure that it's available.
Is the field on your User model that holds the email called email?
I'm using Rspec to test in Rails. I'm trying to get the test to click on the edit button so that it can edit the todo list. Since there are multiple edit links I added a dom_id in my index.html.erb file:
<h1>Listing Todo Lists</h1>
<table>
<thead>
<tr>
<th>Title</th>
<th>Description</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #todo_lists.each do |todo_list| %>
<tr id="<%= dom_id(todo_list) %>">
<td><%= todo_list.title %></td>
<td><%= todo_list.description %></td>
<td><%= link_to 'Show', todo_list %></td>
<td><%= link_to 'Edit', edit_todo_list_path(todo_list) %></td>
<td><%= link_to 'Destroy', todo_list, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Todo list', new_todo_list_path %>
I keep getting the error message:
1) Editing todo lists Updates todo list with correct info
Failure/Error:
within "#todo_list_#{todo_list.id}" do
click_link "Edit"
end
Capybara::ElementNotFound:
Unable to find css "#todo_list_"
# ./spec/features/todo_lists/edit_spec.rb:11:in `update_todo_list'
# ./spec/features/todo_lists/edit_spec.rb:24:in `block (2 levels) in <top (required)>'
This is my edit_spec.rb file:
require 'rails_helper'
describe "Editing todo lists" do
def update_todo_list(options={})
options[:title] ||= "My todo list"
options[:description] ||= "This is my todo list"
todo_list = options[:todo_list]
visit "/todo_lists"
within "#todo_list_#{todo_list.id}" do
click_link "Edit"
end
fill_in "Title", with: options[:title]
fill_in "Description", with: options[:description]
click_button "Update Todo list"
end
it "Updates todo list with correct info" do
todo_list = TodoList.new(title: "Grocery list", description: "This is my grocery list")
update_todo_list(todo_list: todo_list, title: "New title", description: "New description")
todo_list.reload
expect(page).to_have content("Todo list was successfully updated")
expect(todo_list.title).to eq("New title")
expect(todo_list.description).to eq("New description")
end
end
There isn't a problem with my controllers or models because when I try to edit a todo list when the server is launched, it works fine. Any help would be greatly appreciated!
Once you only called new on your model, it wasn't saved and therefore there isn't an id. That's why error is saying:
Unable to find css "#todo_list_"
Thus you must save your model so that it'll have an id to be interpolated on the selector.
it "Updates todo list with correct info" do
todo_list = TodoList.create!(title: "Grocery list", description: "This is my grocery list")
...
I generated a scaffold for a To-Do List app, and I left out some columns to add later.
I ran the command to create a migration to add a new column named client and I changed my files so that it shows on the projects index and form, but when i enter something into the client field and submit, it doesn't save the information and remains blank..
Update 1:
Here is whats in my routes:
'
Rails.application.routes.draw do
root :to => 'projects#index'
resources :projects
end
'
Here is my index view:
'
<h1 id="title">Project List</h1>
<table>
<thead>
<tr id="headers">
<th>Title</th>
<th>Client</th>
<th>Description</th>
<th>Hours</th>
<th>Done</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody class="col-md-2" id="listItems">
<% #projects.each do |project| %>
<tr id="table">
<td><%= project.title %></td>
<td><%= project.client %></td>
<td><%= project.description %></td>
<td><%= project.hours %></td>
<td><%= project.done %></td>
<td><%= link_to " #{image_tag('show.png')}".html_safe, project, id:'showButton' %></td>
<td><%= link_to " #{image_tag('edit.png')}".html_safe, edit_project_path(project), id:'editButton' %></td>
<td><%= link_to " #{image_tag('destroy.png')}".html_safe, project, id:'destroyButton', method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Project', new_project_path, id:"new" %>
<footer id="footer">Copyright 2014 Kira Banks</footer>
'
To keep your application secured, Rails has a feature called Strong Parameters and the docs says:
It provides an interface for protecting attributes from end-user
assignment. This makes Action Controller parameters forbidden to be
used in Active Model mass assignment until they have been whitelisted.
So, basically you need to whitelist the new client attribute in the Projects controller by adding it to the list:
class ProjectsController < ApplicationController
# ...
# at the end of the file
private
def project_params
params.require(:project).permit(:title, :description, :hours, :done, :client)
end
end
I am currently trying to create a shopping cart for my rails application and once populated my cart should be saved in the session.
Everytime i try to run the server to test if working or not, it seems my routes are not working at all...
See below my routes.rb, my cart controller and my index.
Any help will be appreciated.
routes.rb
Sewingsupplies::Application.routes.draw do
get "cart/index"
get "cart/success"
PUT "/cart/:id" => "cart#add"
DELETE "/cart/:id" => "cart#remove"
DELETE "/cart" => "cart#clear"
POST "/cart/checkout"
devise_for :admins
resources :catalogues
devise_for :users
cart controller
def index
sessions[:cart] = {}
render 'cart/index'
end
def success
end
def add
#catalogue = Catalogue.find(params[:id])
cart = sessions[:cart]
cart = {:catalogue => #catalogue.name,category => #catalogue.category, :code => #catalogue.code , :colour=> #catalogue.colour, :description => #catalogue.description, :image => #catalogue.image , :unitprice => += #catalogue.unitprice, :unitquantity => +=1, :unitweight => += #catalogue.unitweight }
sessions[:cart] = cart
render 'cart/add'
end
end
and my index page
<h1>Listing catalogues</h1>
<table>
<tr>
<th>Name</th>
<th>Code</th>
<th>Category</th>
<th>Description</th>
<th>Unitprice</th>
<th>Unitquantity</th>
<th>Unitweight</th>
<th>Colour</th>
<th>Image</th>
<th>User</th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
<% #catalogues.each do |catalogue| %>
<tr>
<td><%= catalogue.name %></td>
<td><%= catalogue.code %></td>
<td><%= catalogue.category %></td>
<td><%= catalogue.description %></td>
<td><%= catalogue.unitprice %></td>
<td><%= catalogue.unitquantity %></td>
<td><%= catalogue.unitweight %></td>
<td><%= catalogue.colour %></td>
<td><%= image_tag(catalogue.image, :width => 150) if catalogue.image.present?%></td>
<td><%= catalogue.user.email %></td>
<td><%= link_to 'Show', catalogue %></td>
<td><%= link_to 'Edit', edit_catalogue_path(catalogue) %></td>
<td><%= link_to 'Destroy', catalogue, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<td><%= link_to "Add to cart", controller: "cart", action: "add", id: #catalogue.id, method: :post %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to "View cart", controller: "cart", action: "index" %>
<%= link_to 'New Catalogue', new_catalogue_path %>
In your routes.rb file you have defined the routes with PUT and DELETE in uppercase. Define the routes in lowercase like put and delete as these are methods of ActionDispatch::Routing::Mapper class which are called to setup routes for your application. As ruby is case sensitive you need to call the method with correct case.
I am trying to write test scenario for delete,but i don't understand why it is not getting destroy link.
Here my test scenario:
Scenario: User can delete kids
Given I am on the kids page
When I Destroy kid
Then I should see "Kid deleted successfully"
Then one kid should not exist
<h1>Listing kids</h1>
<tr>
<th>Kid name</th>
<th colspan=3>Action</th>
</tr>
<% #kids.each do |kid| %>
<tr>
<td><%= kid.kid_name %></td>
<td><%= link_to 'Show', kid %></td>
<td><%= link_to 'Edit', edit_kid_path(kid) %></td>
<td><%= link_to 'Destroy', kid, method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<%= link_to 'New Kid', new_kid_path %>
My step defination for link:
When /^I Destroy kid$/ do |link|
click_link(link)
end
Please somebody suggest step definition for delete link, please correct me if their is some error in my scenario.
Thank you.
I have found the answer for destroy link. It won't support webrat so i have removed webrat and used capybara and my issue get solved.
Here is the step:
Scenario: Delete Kid
Given I am on the kids page
And there is a kid with kid_name "john"
When I destroy that kid
Then I am on the kids page
step_defination:
When /^I destroy that (.*)$/ do |element_type|
element = element_type.classify.constantize.last
path = "#{element_type}_path"
case page.driver
when Capybara::RackTest::Driver
page.driver.submit :delete, send(path, element), {}
else
visit send(path, element, { method: :delete })
end
end </code>