Skip to content

Commit

Permalink
migrate all before(:all) to let and before(:each)
Browse files Browse the repository at this point in the history
  • Loading branch information
gouthamvel committed Mar 19, 2014
1 parent 44ecf45 commit de3fec5
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 79 deletions.
6 changes: 3 additions & 3 deletions spec/factories/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@
fullname { "#{Faker::Name.first_name} #{Faker::Name.last_name}" }
username { Faker::Lorem.characters(9) }
email { Faker::Internet.email }
born_on Date.current.to_s
born_on { Date.current.to_s }
avatar { fixture_file_upload(Rails.root.join(*%w[ spec fixtures files example.jpg ]), 'image/jpg') }
factory :user_with_out_password do
skip_password true
factory :employee do
startup_link_verifier_id 1
startup_verifier_token SecureRandom.hex(30)
startup_verifier_token { SecureRandom.hex(30) }
end
factory :founder do
is_founder true
startup_link_verifier_id 1
startup_verifier_token SecureRandom.hex(30)
startup_verifier_token { SecureRandom.hex(30) }
end
factory :user_with_facebook do
after(:create) do |user, evaluator|
Expand Down
16 changes: 7 additions & 9 deletions spec/mailers/startup_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@
include Rails.application.routes.url_helpers

context 'shoot out emails to all founders' do
before(:all) do
@startup = create :startup
@new_employee = create :user_with_out_password
@email = StartupMailer.respond_to_new_employee(@startup, @new_employee)
end
let(:startup) { create :startup}
let(:new_employee) { create :user_with_out_password}
let(:email) { StartupMailer.respond_to_new_employee(startup, new_employee) }

it "with to set as founders email" do
@email.should deliver_to(@startup.founders.map { |e| "#{e.fullname} <#{e.email}>" })
email.should deliver_to(startup.founders.map { |e| "#{e.fullname} <#{e.email}>" })
end

it "with body containing a link to the confirmation link" do
@email.should have_body_text(/#{confirm_employee_startup_url(@startup, token: @new_employee.startup_verifier_token)}/)
email.should have_body_text(/#{confirm_employee_startup_url(startup, token: new_employee.startup_verifier_token)}/)
end

it "with subject \"Approve new employee at \#{@startup.name}\"" do
@email.should have_subject(/Approve new employee at #{@startup.name}/)
it "with subject \"Approve new employee at startup\"" do
email.should have_subject(/Approve new employee at #{startup.name}/)
end
end
end
40 changes: 19 additions & 21 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,35 +43,33 @@
end

context "verify user as founder" do
before(:all) do
@startup = create(:startup)
@startup2 = create(:startup)
@user = create(:user_with_out_password, startup: @startup)
@user_self = create(:user_with_out_password, startup: @startup, startup_link_verifier: @user)
@user_other = create(:user_with_out_password, startup: @startup, startup_link_verifier: nil)
end
let(:startup) { create(:startup)}
let(:startup2) { create(:startup)}
let(:user) { create(:user_with_out_password, startup: startup)}
let(:user_self) { create(:user_with_out_password, startup: startup, startup_link_verifier: user)}
let(:user_other) { create(:user_with_out_password, startup: startup, startup_link_verifier: nil)}

it "if self is verified and belongs to same startup" do
@user_self.startup = @startup
@user_self.startup_link_verifier = @user
@user_other.startup = @startup
@user_other.startup_link_verifier = false
expect(@user_self.verify(@user_other)).to be_truthy
expect(@user_other.startup_link_verifier).to eql(@user_self)
expect(@user_other.startup_verifier_token).not_to eql(nil)
expect(@user_other.startup_verifier_token).not_to eql("")
user_self.startup = startup
user_self.startup_link_verifier = user
user_other.startup = startup
user_other.startup_link_verifier = false
expect(user_self.verify(user_other)).to be_truthy
expect(user_other.startup_link_verifier).to eql(user_self)
expect(user_other.startup_verifier_token).not_to eql(nil)
expect(user_other.startup_verifier_token).not_to eql("")
end

it "raises exception if self is not verfiied" do
@user_self.update_attributes!(startup_link_verifier: nil)
expect{@user_self.verify(@user_other)}.to raise_exception(/not allowed to verify founders yet/)
user_self.update_attributes!(startup_link_verifier: nil)
expect{user_self.verify(user_other)}.to raise_exception(/not allowed to verify founders yet/)
end

it "raises exception if both user doesn't belongs to same startup" do
@user_self.update_attributes!(startup_link_verifier: @user, startup: nil)
expect{@user_self.verify(@user_other)}.to raise_exception(/not allowed to verify founders of/)
@user_self.update_attributes!(startup_link_verifier: @user, startup: @startup2)
expect{@user_self.verify(@user_other)}.to raise_exception(/not allowed to verify founders of/)
user_self.update_attributes!(startup_link_verifier: user, startup: nil)
expect{user_self.verify(user_other)}.to raise_exception(/not allowed to verify founders of/)
user_self.update_attributes!(startup_link_verifier: user, startup: startup2)
expect{user_self.verify(user_other)}.to raise_exception(/not allowed to verify founders of/)
end
end
end
23 changes: 10 additions & 13 deletions spec/requests/startups_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,31 @@
describe "Startups" do
include V1ApiSpecHelper

describe "GET /startups" do
it "works! (now write some real specs)" do
# Run the generator again with the --webrat flag if you want to use webrat methods/matchers
get "/api/startups"
expect(response.status).to eq(200)
end
end
# describe "GET /startups" do
# it "works! (now write some real specs)" do
# # Run the generator again with the --webrat flag if you want to use webrat methods/matchers
# get "/api/startups"
# expect(response.status).to eq(200)
# end
# end

describe "Accept GET on confirm_employee" do
let(:startup) { create :startup }
let(:new_employee){ create :employee, {startup_link_verifier_id: nil, startup: startup}}
before(:each) do
startup = create :startup
founder = startup.founders.first
login(founder)
expect(Urbanairship).to receive(:push).and_return true
allow(Urbanairship).to receive(:push).and_return true
post "/startups/#{startup.id}/confirm_employee", {token: new_employee.startup_verifier_token, is_founder: true}
new_employee.reload
startup.reload
end

it "responds with 200 status" do
expect(response.status).to eql(200)
end

it "assigns requested employee to startup" do
employees_ids = startup.reload.employees.map &:id
expect(employees_ids).to include(new_employee.id)
new_employee.reload
expect(startup.employees).to include(new_employee)
expect(new_employee.is_founder).to be(true)
end

Expand Down
43 changes: 20 additions & 23 deletions spec/requests/v1/startup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
include UserSpecHelper
include Rails.application.routes.url_helpers

before(:all) do
@startup = create(:startup, {name: 'startup 1'})
@startup1 = create(:startup, {name: 'startup 2'})
@startup2 = create(:startup, {name: 'foobar 1'})
@startup3 = create(:startup, {name: 'foobar 2'})
end
let!(:startup ) { create(:startup, {name: 'startup 1'}) }
let!(:startup1) { create(:startup, {name: 'startup 2'}) }
let!(:startup2) { create(:startup, {name: 'foobar 1'}) }
let!(:startup3) { create(:startup, {name: 'foobar 2'}) }

it "fetch startups on index" do
get "/api/startups", {},version_header
Expand All @@ -24,7 +22,7 @@
end

it "fetch startups within a category" do
get "/api/startups", {category: @startup1.categories.first.name},version_header
get "/api/startups", {category: startup1.categories.first.name},version_header
expect(response).to render_template(:index)
response.body.should have_json_size(1).at_path("/")
response.body.should have_json_path("0/id")
Expand All @@ -48,7 +46,7 @@
end

it "fetches one startup with " do
get "/api/startups/#{@startup.id}", {}, version_header
get "/api/startups/#{startup.id}", {}, version_header
expect(response).to render_template(:show)
response.body.should have_json_path("id")
response.body.should have_json_path("name")
Expand Down Expand Up @@ -78,33 +76,32 @@

it "fetches suggestions based on given term" do
get "/api/startups/load_suggestions", {term: 'fo'}, version_header
response.body.should have_json_path("0/id")
response.body.should have_json_path("0/name")
response.body.should have_json_path("0/logo_url")
expect(response.body).to have_json_size(2).at_path("/")
expect(response.body).to have_json_path("0/id")
expect(response.body).to have_json_path("0/name")
expect(response.body).to have_json_path("0/logo_url")
end

context "request to add new founder to a startup" do
before(:all) do
@startup = create :startup
@new_employee = create :user_with_out_password
end
let(:startup) { create :startup}
let(:new_employee) { create :user_with_out_password}

before(:each) do
ActionMailer::Base.deliveries = []
end

it "raise error if auth_token is not given" do
expect { post "/api/startups/#{@startup.id}/link_employee", {employee_id: @new_employee.id}, {} }.to raise_error(RuntimeError)
expect { post "/api/startups/#{startup.id}/link_employee", {employee_id: new_employee.id}, {} }.to raise_error(RuntimeError)
end

it "sends email to all existing co-founders" do
post "/api/startups/#{@startup.id}/link_employee", {position: 'startup ceo'}, version_header(@new_employee)
@new_employee.reload
expect(emails_sent.last).to have_subject(/Approve new employee at #{@startup.name}/)
expect(emails_sent.last.body.to_s).to include(confirm_employee_startup_url(@startup, token: @new_employee.startup_verifier_token))
expect(@new_employee.startup_link_verifier_id).to eql(nil)
expect(@new_employee.title).to eql('startup ceo')
expect(@new_employee.reload.startup_id).to eql(@startup.id)
post "/api/startups/#{startup.id}/link_employee", {position: 'startup ceo'}, version_header(new_employee)
new_employee.reload
expect(emails_sent.last).to have_subject(/Approve new employee at #{startup.name}/)
expect(emails_sent.last.body.to_s).to include(confirm_employee_startup_url(startup, token: new_employee.startup_verifier_token))
expect(new_employee.startup_link_verifier_id).to eql(nil)
expect(new_employee.title).to eql('startup ceo')
expect(new_employee.reload.startup_id).to eql(startup.id)
expect(response).to be_success
have_user_object(response, 'user')
end
Expand Down
14 changes: 7 additions & 7 deletions spec/requests/v1/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
end

context "when startup is just created" do
before(:all) do
@startup = create :startup
get "/api/users/self", {}, version_header(@startup.founders.first)
let(:startup) { create :startup }
before(:each) do
get "/api/users/self", {}, version_header(startup.founders.first)
end
it "should have incorporation enabled, message nil" do
expect(parse_json(response.body, 'startup_meta_details/incorporation/is_enabled')).to eq(true)
Expand All @@ -54,15 +54,15 @@
end

context "profileinfo is submited" do
before(:all) do
@startup = create :startup
@founder = @startup.founders.each do |f|
let(:startup) { startup = create :startup }
before(:each) do
startup.founders.each do |f|
f.update_attributes!(
address: create(:address),
father: create(:name),
)
end
get "/api/users/self", {}, version_header(@startup.founders.first)
get "/api/users/self", {}, version_header(startup.founders.first)
end
it "should have incorporation enabled" do
expect(parse_json(response.body, 'startup_meta_details/incorporation/is_enabled')).to eq(true)
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
config.include(JsonSpec::Helpers)
config.include(EmailSpec::Helpers)
config.include(EmailSpec::Matchers)
config.before(:suite) do
config.before(:each) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
DatabaseCleaner.start
Expand Down
4 changes: 2 additions & 2 deletions spec/views/startups/show.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
expect(rendered).to match(/Email/)
expect(rendered).to match(/Phone/)
expect(rendered).to match(/Categories/)
expect(rendered).to match(/Facebook Link/)
expect(rendered).to match(/Twitter Link/)
expect(rendered).to match(/Facebook/)
expect(rendered).to match(/Twitter/)
end
end

0 comments on commit de3fec5

Please sign in to comment.