Skip to content

Instantly share code, notes, and snippets.

@swapnilchincholkar
Last active December 28, 2015 15:49
Show Gist options
  • Save swapnilchincholkar/7524215 to your computer and use it in GitHub Desktop.
Save swapnilchincholkar/7524215 to your computer and use it in GitHub Desktop.
Using default parameter while writing test cases.
class User
field :first_name, type: String
field :company_name, type: String
field :role, type: String
def name
role == 'admin' ? first_name : [company_name, first_name].join(':')
end
end
require 'spec_helper'
describe User do
# here an argument is passed (default argument), using which we
# manipulate our condition hence we don't need to add 2 different test case for similar scenarios
shared_examples "check user's name" do |role|
it "should return users full name" do
name = role == 'admin' ? @user.first_name : [@user.company_name, @user.first_name].join(':')
@user.name.should == name
end
end
contect "should check user object" do
before(:each) do
@admin_user = Factory(:admin_user)
@user = Factory(:user)
end
# here we call same shared example in 2 different scenario using default argument
include_examples "check user's name", @admin_user.role
include_examples "check user's name", @user.role
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment