Skip to content

Instantly share code, notes, and snippets.

@pacoguzman
Created February 11, 2010 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pacoguzman/301419 to your computer and use it in GitHub Desktop.
Save pacoguzman/301419 to your computer and use it in GitHub Desktop.
Ejemplos Testing en Ruby y Rails con Rspec
require 'spec_helper'
describe Member::GraffitiesController do
context "A anonymous user" do
it "denies access to create action" do
@controller.expects(:create).never
post :create, {:wall_id => 1}
end
it "denies access to reply action" do
@controller.expects(:reply).never
post :reply, {:wall_id => 1}
end
it "denies access to like action" do
@controller.expects(:like).never
post :like, {:wall_id => 1}
end
it "denies access to update action" do
@controller.expects(:update).never
put :reply, {:wall_id => 1}
end
it "denies access to destroy action" do
@controller.expects(:destroy).never
delete :reply, {:wall_id => 1}
end
end
context "A logged User, not a friend of the owner" do
before(:each) do
@member = Factory(:member).profile
@owner = Factory(:member, :login => "Berlusconi").profile
@request.session[:user_id] = @member.user.id
@request.stubs(:referer => "/")
end
context "on POST to :create" do
before(:each) do
post :create, {:wall_id => @owner.wall.id, :graffity => Factory.attributes_for(:graffity)}
end
it "not create a graffity" do
@owner.wall.graffities.should be_empty
end
it "set a flash error message" do
flash[:error].should_not be_empty
end
end
context "ON POST to :reply" do
before(:each) do
@graffity = Factory(:graffity, :wall => @owner.wall, :profile => @owner)
@suplanted_owner = Factory(:member, :login => "Merkel").profile
end
context "trying to write a graffity in other wall" do
before(:each) do
post :reply, {:id => @graffity.id, :graffity => Factory.attributes_for(:graffity)}
end
it "not create any graffity in the owner wall" do
@owner.wall.should have(1).graffity
end
it "not create any graffity in the suplanted owner wall" do
@suplanted_owner.wall.graffities.should be_empty
end
end
context "with correct params" do
before(:each) do
post :reply, {:id => @graffity.id, :graffity => Factory.attributes_for(:graffity)}
end
it "not create a reply" do
@graffity.reload.replies.should be_empty
end
it "set a error flash message" do
flash[:error].should_not be_empty
end
end
end
context "ON GET :like" do
before(:each) do
@graffity = Factory(:graffity, :wall => @owner.wall, :profile => @owner)
end
context "with correct params" do
before(:each) do
get :like, {:id => @graffity.id}
end
it "not create a like" do
@graffity.reload.likes.should be_empty
end
it "set a error flash message" do
flash[:error].should_not be_empty
end
end
end
end
context "A logged friend user" do
before(:each) do
@member = Factory(:member).profile
@owner = Factory(:member, :login => "Berlusconi").profile
@request.session[:user_id] = @member.user.id
@request.stubs(:referer => "/")
@owner.add_friend(@member)
end
context "ON POST :create" do
context "with valid graffity params" do
before(:each) do
post :create, {:wall_id => @owner.wall.id, :graffity => Factory.attributes_for(:graffity)}
end
it "create a graffity in the owner wall" do
@owner.wall.should have(1).graffities
end
it "set a ok flash message" do
flash[:ok].should_not be_empty
end
end
context "without comment" do
before(:each) do
post :create, {:wall_id => @owner.wall.id, :graffity => {}}
end
it "not create a graffity" do
@owner.wall.should have(0).graffities
end
it "set a error flash message" do
flash[:error].should_not be_empty
end
end
end
context "ON POST :reply" do
before(:each) do
@graffity = Factory(:graffity, :wall => @owner.wall, :profile => @owner)
end
context "with valid reply params" do
before(:each) do
post :reply, {:id => @graffity.id, :graffity => Factory.attributes_for(:graffity)}
end
it "create a reply" do
@owner.wall.graffities.first.should have(1).replies
end
it "set a ok flash message" do
flash[:ok].should_not be_empty
end
end
end
context "ON GET :like" do
before(:each) do
@graffity = Factory(:graffity, :wall => @owner.wall, :profile => @owner)
end
context "with valid params" do
before(:each) do
get :like, {:id => @graffity.id}
end
it "create a like" do
@owner.wall.graffities.first.should have(1).like
end
it "set a ok flash message" do
flash[:ok].should_not be_empty
end
end
end
end
end
require 'spec_helper'
describe Graffity do
# it { should belong_to(:wall) }
# it { should belong_to(:profile) }
# it { should have_many(:replys) }
it "is not valid without a comment" do
@owner = Factory(:member, :login => "ownerious")
@visitor = Factory(:member, :login => "visitorious")
graffity = Graffity.new(:wall => @owner.profile.wall, :profile => @visitor.profile)
graffity.should_not be_valid
end
describe "build_from" do
before(:each) do
@wall = Wall.new
@wall.stub(:profile)
end
it "should return a graffity" do
Graffity.build_from(@wall, nil, {}).should be_instance_of(Graffity)
end
it "should set type_common as true by default" do
graffity = Graffity.build_from(@wall, nil, {})
graffity.type_common.should be_true
end
it "passing type_common as false should override the default type_common" do
graffity = Graffity.build_from(@wall, nil, {:type_common => false})
graffity.type_common.should be_false
end
it "should set wall with the wall passed" do
graffity = Graffity.build_from(@wall, nil, {})
graffity.wall.should == @wall
end
it "should set profile with the profile passed" do
profile = Profile.new
graffity = Graffity.build_from(@wall, profile, {})
graffity.profile.should == profile
end
it "should set title with the title passed" do
graffity = Graffity.build_from(@wall, nil, {:title => "Hello, world"})
graffity.title.should == "Hello, world"
end
it "should set comment with the comment passed" do
graffity = Graffity.build_from(@wall, nil, {:comment => "Bazzinga!"})
graffity.comment.should == "Bazzinga!"
end
end
# Subject-ivity
describe "subject-ivity" do
subject { Graffity.new(:type_common => true) }
specify { subject.should be_common_graffity }
specify { subject.should_not be_like_graffity }
end
describe "implicit subject-ivity" do
subject { Graffity.new(:type_common => true) }
specify { should be_common_graffity }
specify { should_not be_like_graffity }
end
end
require 'spec_helper'
describe UserMailer do
before(:each) do
ActionMailer::Base.deliveries = []
@user = Factory.build(:user)
@user.activation_code = 123456
end
describe "signup notification" do
before(:each) do
UserMailer.deliver_signup_notification(@user)
end
it "include the activation_code in the body" do
#ActionMailer::Base.deliveries.first.body.should match(/#{@user.activation_code}/)
first_delivery.body.should match(/#{@user.activation_code}/)
end
it "include the activate_account message in the subject" do
first_delivery.subject.should include(I18n.t("tog_user.mailer.activate_account"))
end
end
describe "activation" do
before(:each) do
UserMailer.deliver_activation(@user)
end
it "include the account_activated message in the subject subject" do
first_delivery.subject.should include(I18n.t("tog_user.mailer.account_activated"))
end
end
describe "reset notification" do
before(:each) do
@user.stub(:password_reset_code).and_return(123456)
UserMailer.deliver_reset_notification(@user)
end
it "receive an email with a link to reset the password" do
first_delivery.body.should include(@user.password_reset_code)
end
it "include the reset_password message in the subject subject" do
first_delivery.subject.should include(I18n.t("tog_user.mailer.reset_password"))
end
end
protected
def first_delivery
ActionMailer::Base.deliveries.first
end
def last_delivery
ActionMailer::Base.deliveries.last
end
end
require 'spec_helper'
describe User do
describe "creating a user" do
it "changes the number of users by one" do
lambda {Factory(:user)}.should change{User.count}.by(1)
end
end
describe "creating a member" do
it "changes the number of active user from 0 to 1" do
lambda {Factory(:member)}.should change{User.count}.from(0).to(1)
end
end
describe "authentication" do
before do
@login = 'dickdivers'
@email = 'dick@divers.com'
@password = 'nightingale'
end
context "of not active user" do
before(:each) do
@user = Factory(:user, :login => @login, :email => @email,
:password => @password, :password_confirmation => @password)
end
it "return nil for correct login attempts using login column" do
Tog::Config["plugins.tog_user.email_as_login"] = false
User.authenticate(@login, @password).should be_nil
end
it "return nil for correct login attempts using email column" do
Tog::Config["plugins.tog_user.email_as_login"] = true
User.authenticate(@email, @password).should be_nil
end
end
context "of a member" do
before(:each) do
@user = Factory(:member, :login => @login, :email => @email,
:password => @password, :password_confirmation => @password)
end
it "return nil for incorrect login attempts" do
Tog::Config["plugins.tog_user.email_as_login"] = false
User.authenticate(@login, "bad_password").should be_nil
User.authenticate("bad_login_email", "badpass").should be_nil
User.authenticate("", "").should be_nil
User.authenticate(nil, nil).should be_nil
Tog::Config["plugins.tog_user.email_as_login"] = true
User.authenticate(@email, "bad_password").should be_nil
User.authenticate("bad_login_email", "badpass").should be_nil
User.authenticate("", "").should be_nil
User.authenticate(nil, nil).should be_nil
end
context "using email" do
before(:each) do
Tog::Config["plugins.tog_user.email_as_login"] = true
end
it "return the user object for a valid login using his email" do
User.authenticate(@email, @password).should == @user
User.authenticate(@email.upcase, @password).should == @user
end
end
context "using login" do
before(:each) do
Tog::Config["plugins.tog_user.email_as_login"] = false
end
it "return the user object for a valid login using his login" do
User.authenticate(@login, @password).should == @user
User.authenticate(@login.upcase, @password).should == @user
end
end
context "using login or email without check out email_as_login property" do
before(:each) do
Tog::Config.should_receive(:[]).never
end
it "return the user object for a valid login using his login" do
User.authenticate(@login, @password).should == @user
end
it "return the user object for a valid login using his email" do
User.authenticate(@email, @password).should == @user
end
end
end
end
describe "register" do
before(:each) do
@user = Factory.build(:user)
end
it "should be in state pending" do
@user.register!
@user.state.should == "pending"
end
it "generate an activation code" do
@user.register!
@user.activation_code.should_not be_nil
end
it "receive an signup notification" do
UserMailer.stub!(:deliver_signup_notification)
UserMailer.should_receive(:deliver_signup_notification).with(@user)
@user.register!
end
end
describe "a registered user activates his account" do
before(:each) do
@user = Factory(:registered)
end
it "should be in state active" do
@user.activate!
@user.state.should == "active"
end
it "receive a welcome message (activation)" do
UserMailer.stub!(:deliver_activation)
UserMailer.should_receive(:deliver_activation).with(@user)
@user.activate!
end
end
describe "a member has forgotten his password" do
before(:each) do
@user = Factory(:member)
end
it "generate a password reset code" do
@user.forgot_password
@user.password_reset_code.should_not be_nil
end
it "receive an email to reset the password (reset notification)" do
UserMailer.stub!(:deliver_reset_notification)
UserMailer.should_receive(:deliver_reset_notification).with(@user)
@user.forgot_password
end
end
desrcibe "trying activate without register" do
it "raise an invalid transition exception (AASM::InvalidTransition)" do
lambda {
Factory(:user).activate!
}.should raise_exception(AASM::InvalidTransition)
end
end
describe "validations" do
context "authentication patches" do
it "not be valid include @.- in the login"
# it "not be valid include @.- in the login" do
# pending "require authentication patches to use login in the profile url"
# end
#
# it "not be valid include @.- in the login" do
# pending "require authentication patches to use login in the profile url" do
# %w(Holden@com Holde.com Holde-com).each do |login|
# user = Factory.build(:user, :login => login)
# user.should_not be_valid
# user.errors.on(:login).should_not be_empty
# end
# end
# end
end
end
end
require 'spec_helper'
describe Member::WallsController do
#should_route :get, "/member/wall", :controller => "member/walls", :action => "show"
# describe "show action" do
# before(:each) do
# @member = Factory(:member)
# @owner = @member.profile
# @wall = @owner.wall
# @graffities = @wall.graffities
# @request.session[:user_id] = @member.id
# get :show
# end
#
# # should_assign_to(:owner) {@owner}
# it "assigns @owner" do
# assigns[:owner].should == @owner
# end
#
# # should_assign_to(:wall) {@wall}
# it "assigns @wall" do
# assigns[:wall].should == @wall
# end
#
# # should_assign_to(:graffities) {@graffities}
# it "assigns @graffities" do
# assigns[:graffities].should == @graffities
# end
#
# # should_respond_with :success
# it "respond with success" do
# response.should be_success
# end
#
# # should_not_set_the_flash
# it "not set the flash" do
# flash.should be_empty
# end
#
# # should_render_template 'walls/show'
# it "render template 'walls/show'" do
# response.should render_template('walls/show')
# end
# end
describe "show action mocked" do
before(:each) do
ApplicationController.send(:public, :logged_in?, :current_user, :authorized?)
controller.stub(:login_required).and_return(true)
@graffities = [mock_model(Graffity), mock_model(Graffity)]
@wall = mock_model(Wall)
@owner = mock_model(Profile, :wall => @wall)
@controller.current_user.stub(:profile).and_return(@owner)
end
# should_assign_to(:owner) {@owner}
it "assigns @owner" do
get :show
assigns[:owner].should == @owner
end
# should_assign_to(:wall) {@wall}
it "assigns @wall" do
get :show
assigns[:wall].should == @wall
end
# should_assign_to(:graffities) {@graffities}
it "assigns @graffities" do
Wall.stub!(:get_graffities_for).with(@wall).and_return(@graffities)
get :show
assigns[:graffities].should == @graffities
end
# should_respond_with :success
it "respond with success" do
get :show
response.should be_success
end
# should_not_set_the_flash
it "not set the flash" do
get :show
flash.should be_empty
end
# should_render_template 'walls/show'
it "render template 'walls/show'" do
get :show
response.should render_template('walls/show')
end
end
end
require 'spec_helper'
describe Wall do
# it { should have_many(:graffities) }
# it { should belong_to(:profile) }
it "should be able to create two graffities as root in the same wall" do
@owner = Factory(:member).profile
@wall = @owner.wall
@graffity1 = Factory(:graffity, :profile => @owner, :wall => @wall)
@graffity2 = Factory(:graffity, :profile => @owner, :wall => @wall)
@owner.wall.should have(2).graffities # @owner.wall.graffities.should have(2).items
@owner.wall.graffities.each {|g| g.should be_root}
end
describe "#get_graffities_for" do
before(:each) do
@owner = Factory(:member).profile
@wall = @owner.wall
@graffity1 = Factory(:graffity, :profile => @owner, :wall => @wall)
@graffity2 = Factory(:graffity, :profile => @owner, :wall => @wall)
@friend = Factory(:member, :login => "friend").profile
@graffity3 = Factory(:graffity, :profile => @friend, :wall => @friend.wall)
@graffity4 = Factory(:graffity, :profile => @friend, :wall => @friend.wall)
end
it "passing one wall find all graffities in that wall" do
graffities = Wall.get_graffities_for(@wall)
@wall.graffities.each do |graffity|
graffities.should include(graffity)
end
end
it "passing an array of walls find all graffities in these walls" do
graffities = Wall.get_graffities_for([@wall, @friend.wall])
[@wall.graffities, @friend.wall.graffities].flatten.each do |graffity|
graffities.should include(graffity)
end
end
it "passing one wall and after option find graffities with id higher than the specified in the after option" do
graffities = Wall.get_graffities_for(@wall, :after => @graffity1.id)
graffities.each do |graffity|
graffity.id.should be > (@graffity1.id) # > @graffity1.id
end
end
it "passing one wall and after option find graffities with id lower than the specified in the after option" do
graffities = Wall.get_graffities_for(@wall, :before => @graffity4)
graffities.each do |graffity|
graffity.id.should be < (@graffity4.id) # < @graffity4.id
end
end
it "should order the graffities by descendant id by default" do
Graffity.should_receive(:find).with(:all, hash_including(:order => "id DESC"))
Wall.get_graffities_for(@wall)
end
it "should limit the number of graffities by default" do
Graffity.should_receive(:find).with(:all, hash_including(:limit => Tog::Config['plugins.tog_wall.wall.pagination_size']))
Wall.get_graffities_for(@wall)
end
it "passing a limit option override the default limit" do
Graffity.should_receive(:find).with(:all, hash_including(:limit => 1))
Wall.get_graffities_for(@wall, :limit => 1)
end
end
describe "#scoped_graffities_for" do
it "without passing an option not set an id boundary" do
Wall.scoped_graffities_for([]).proxy_options.should == {:conditions => []}
end
it "passing after option set a lower limiit" do
Wall.scoped_graffities_for([], :after => 1).proxy_options.should == {:conditions => ["id > ?", 1]}
end
it "passing before option set a higher limit" do
Wall.scoped_graffities_for([], :before => 2).proxy_options.should == {:conditions => ["id < ?", 2]}
end
end
# Generated descriptions!
describe "after add two graffities" do
before(:each) do
@owner = Factory(:member).profile
@wall = @owner.wall
@graffity1 = Factory(:graffity, :profile => @owner, :wall => @wall)
@graffity2 = Factory(:graffity, :profile => @owner, :wall => @wall)
end
specify { @wall.should have(2).graffities } # autogenerated descriptions
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment