Last active
December 15, 2015 20:29
-
-
Save shotty01/5318859 to your computer and use it in GitHub Desktop.
view template rspec file - working. Switched to factory girl for data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FactoryGirl.define do | |
factory :user do | |
login "login" | |
firstname Faker::Name.first_name | |
lastname Faker::Name.last_name | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
require 'will_paginate/array' | |
describe "users/index" do | |
before(:each) do | |
# i need this because i use a private model method as a helper method so I have to expose this | |
# i don't remember exactly was in one of ryan's railcasts though (sorting) | |
controller.singleton_class.class_eval do | |
protected | |
def sort_column | |
"id" | |
end | |
helper_method :sort_column | |
end | |
# use timecop freeze as soon as it is available - just to be sure | |
# Timecop.freeze | |
@user1 = create(:user) | |
@user2 = create(:user, :login => "keimchri") | |
#Timecop.return | |
assign(:users, [@user1, @user2]) | |
view.stub(:will_paginate) | |
end | |
it "renders a list of users" do | |
render | |
# rendered.should contain("TEXT") <-- also possible | |
#i dunno yet what i really want to test here | |
assert_select "tr>td", :text => "login".to_s, :count => 1 | |
assert_select "tr>td", :text => "keimchri".to_s, :count => 1 | |
# faker generates same name when called in same test WHY? | |
assert_select "tr>td", :text => @user1.firstname, :count => 2 | |
assert_select "tr>td", :text => @user1.lastname, :count => 2 | |
# created at is the same for both created users | |
assert_select "tr>td", :text => l(@user1.created_at.in_time_zone, :format => :standard).to_s, :count => 2 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Probably not what you want to hear, but I avoid view specs whenever possible. They're too brittle and I find that I have to go look at the page in a browser anyway to confirm. If you have to test it, though, I think you should be able to use Factory Girl's build_stubbed to create your users, then assign the stubbed users to :users as you do in line 23.
One other thing I like to do is always assign an ID in the DOM for whatever I'm iterating through. So in this case each would have a unique ID, then I use Capybara to test that within that element, my page has whatever content I'm looking for. I like the flexibility of this--if I switch from a table to an unordered list or straight