Created
March 12, 2014 13:29
-
-
Save sseletskyy/9506926 to your computer and use it in GitHub Desktop.
RSpec snippets.
More info at http://karolgalanciak.com/blog/2014/03/03/test-driven-rails-part-2/
This file contains 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
describe ArticlesController do | |
describe "inactive article" do | |
let(:article) { double(:article, active?: false, id: 1) } | |
before(:each) do | |
allow(Article).to receive(:find) { article } | |
end | |
describe "user is logged in" do | |
context "user is an admin" do | |
let(:user) { double(:user, admin?: true) } | |
it "renders article" do | |
allow(controller).to receive(:current_user).and_return(admin) | |
get :show, id: article.id | |
expect(response).to render_template :show | |
end | |
end | |
context "user is not an admin" do | |
let(:user) { double(User, admin?: false) } | |
it "redirects to articles path" do | |
allow(controller).to receive(:current_user).and_return(user) | |
get :show, id: article.id | |
expect(response).to redirect_to articles_path | |
end | |
end | |
context "user is not logged in" do | |
before(:each) do | |
allow(controller).to receive(:current_user).and_return(nil) | |
end | |
it "redirects to articles path" do | |
get :show, id: article.id | |
expect(flash[:error]).not_to be nil | |
expect(response).to redirect_to root_path | |
end | |
end | |
end | |
end | |
end |
This file contains 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
feature "User Registration" do | |
context "when visiting new user path" do | |
background do | |
visit new_user_path | |
end | |
context "registering with valid data" do | |
given(:email) { "[email protected]" } | |
background do | |
fill_form_with_valid_data(email: email) | |
end | |
scenario "new user is created" do | |
expect do | |
register | |
end.to change(User, :count).by(1) | |
end | |
context "notifications" do | |
background do | |
register | |
end | |
scenario "confirmation email is sent to the user" do | |
expect(all_email_addresses).to include email | |
end | |
scenario "notification is sent to the admin" do | |
expect(all_email_addresses).to include "[email protected]" | |
end | |
scenario "2 emails are sent" do | |
expect(all_emails.count).to eq 2 | |
end | |
end | |
end | |
end | |
end | |
def fill_form_with_valid_data(args={}) | |
email = args.fetch(:email, "[email protected]") | |
fill_in "email", with: email | |
fill_in "user_password", with: "my-super-secret-password" | |
fill_in "user_password_confirmation", with: "my-super-secret-password" | |
fill_in "age", with: 22 | |
select "Poland", from: "country" | |
check "policy" | |
end | |
def register | |
click_button "Register" | |
end |
This file contains 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
module MailerMacros | |
def last_email | |
ActionMailer::Base.deliveries.last | |
end | |
def last_email_address | |
last_email.to.join | |
end | |
def reset_email | |
ActionMailer::Base.deliveries = [] | |
end | |
def reset_with_delayed_job_deliveries | |
ActionMailer::Base.deliveries = [] | |
end | |
def all_emails | |
ActionMailer::Base.deliveries | |
end | |
def all_emails_sent_count | |
ActionMailer::Base.deliveries.count | |
end | |
def all_email_addresses | |
all_emails.map(&:to).flatten | |
end | |
end |
This file contains 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
let(:user) { FactoryGirl.create(:inactive_user) } | |
describe “#activate” do | |
it “makes user active” do | |
expect(user.activate).to change { user.active }.from(false).to(true) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment