Skip to content

Instantly share code, notes, and snippets.

@patrickcurl
Created December 24, 2012 18:38
Show Gist options
  • Save patrickcurl/4370312 to your computer and use it in GitHub Desktop.
Save patrickcurl/4370312 to your computer and use it in GitHub Desktop.
Rspec problems...
#ERROR: Failures:
# 1) users/index renders a list of users
# Failure/Error: stub_model(User,
# ActiveRecord::StatementInvalid:
# Could not find table 'users'
# ./spec/views/users/index.html.erb_spec.rb:6:in `block (2 levels) in <top (required)>'
#app/models/user.rb
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# email :string(255)
# password_digest :string(255)
# mobile :string(255)
# first_name :string(255)
# last_name :string(255)
# address :string(255)
# city :string(255)
# state :string(255)
# zip :string(255)
# auth_token :string(255)
# password_reset_token :string(255)
# password_reset_sent_at :datetime
# created_at :datetime not null
# updated_at :datetime not null
#
class User < ActiveRecord::Base
has_secure_password
validates_presence_of :password, :on => :create
validates_confirmation_of :password
validates_presence_of :email, :on => :create
validates_uniqueness_of :email
validates_presence_of :mobile, :on => :create
before_create { generate_token(:auth_token)}
attr_accessible :address, :city, :email, :first_name, :last_name, :mobile, :password,
:password_confirmation, :state, :zip, :login
def generate_token(column)
begin
self[column] = SecureRandom.urlsafe_base64
end while User.exists?(column => self[column])
end
end
#app/views/users/index.html.erb
<h1>Listing users</h1>
<table>
<tr>
<th>Email</th>
<th>Mobile</th>
<th>First name</th>
<th>Last name</th>
<th>Address</th>
<th>City</th>
<th>State</th>
<th>Zip</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @users.each do |user| %>
<tr>
<td><%= user.email %></td>
<td><%= user.mobile %></td>
<td><%= user.first_name %></td>
<td><%= user.last_name %></td>
<td><%= user.address %></td>
<td><%= user.city %></td>
<td><%= user.state %></td>
<td><%= user.zip %></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 %>
</table>
<br />
<%= link_to 'New User', new_user_path %>
#spec/views/users/index.html.erb_spec.rb
require 'spec_helper'
describe "users/index" do
before(:each) do
assign(:users, [
stub_model(User,
:email => "[email protected]",
# :password_digest => "Password Digest",
:password => "123456",
:password_confirmation => "123456",
:mobile => "6666666666",
:first_name => "First",
:last_name => "Last",
:address => "Address",
:city => "City",
:state => "State",
:zip => "Zip",
# :auth_token => "Auth Token",
# :password_reset_token => "Password Reset Token"
),
stub_model(User,
:email => "[email protected]",
# :password_digest => "Password Digest",
:password => "123456",
:password_confirmation => "123456",
:mobile => "5555555555",
:first_name => "First Name",
:last_name => "Last Name",
:address => "Address",
:city => "City",
:state => "State",
:zip => "Zip",
# :auth_token => "Auth Token",
# :password_reset_token => "Password Reset Token"
)
])
end
it "renders a list of users" do
render
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "tr>td", :text => "Email".to_s, :count => 2
# assert_select "tr>td", :text => "Password Digest".to_s, :count => 2
assert_select "tr>td", :text => "Mobile".to_s, :count => 2
assert_select "tr>td", :text => "First Name".to_s, :count => 2
assert_select "tr>td", :text => "Last Name".to_s, :count => 2
assert_select "tr>td", :text => "Address".to_s, :count => 2
assert_select "tr>td", :text => "City".to_s, :count => 2
assert_select "tr>td", :text => "State".to_s, :count => 2
assert_select "tr>td", :text => "Zip".to_s, :count => 2
# assert_select "tr>td", :text => "Auth Token".to_s, :count => 2
# assert_select "tr>td", :text => "Password Reset Token".to_s, :count => 2
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment