Skip to content

Instantly share code, notes, and snippets.

@stevo
Created October 5, 2017 09:02
Show Gist options
  • Save stevo/b8748f45cd863796931433a882a8202a to your computer and use it in GitHub Desktop.
Save stevo/b8748f45cd863796931433a882a8202a to your computer and use it in GitHub Desktop.
Keeping everything in "it"
# Before - using subject, let, and before
describe User do
subject { create(:user) }
describe "#latest_tasks" do
context "when tasks exist for user and another user" do
let(:another_user) { create(:user) }
before do
create_list(:task, 3, user: subject)
create_list(:task, 5, user: another_user)
end
context "when user is admin" do
subject { create(:user, :admin) }
it "returns all tasks for all users" do
expect(subject.latest_tasks.size).to eq 8
end
context "when other user is superadmin" do
let(:another_user) { create(:user, :super_admin) }
it "returns tasks only for admin" do
expect(subject.latest_tasks.size).to eq 3
end
end
end
end
end
end
# After - whole setup is within an example
describe User do
describe "#latest_tasks" do
context "when tasks exist for user and another user" do
context "when user is admin" do
it "returns all tasks for all users" do
admin = create(:user, :admin)
create_list(:task, 3, user: admin)
create_list(:task, 5, user: create(:user))
expect(admin.latest_tasks.size).to eq 8
end
context "when other user is superadmin" do
it "returns tasks only for admin" do
admin = create(:user, :admin)
create_list(:task, 3, user: admin)
create_list(:task, 5, user: create(:user, :superadmin))
expect(admin.latest_tasks.size).to eq 3
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment