From f92e67b7b5fe2ddde56ae8aa2796d53c71c4b490 Mon Sep 17 00:00:00 2001 From: Vinh Nguyen Date: Wed, 7 Oct 2015 11:39:49 +0700 Subject: [PATCH 1/3] Change new Hash style --- spec/controllers/subscriptions_controller_spec.rb | 2 +- spec/helpers/application_helper_spec.rb | 6 +++--- spec/jobs/headline_scraper_job_spec.rb | 12 ++++++------ spec/mailers/news_mailer_spec.rb | 10 +++++----- spec/mailers/subscription_mailer_spec.rb | 2 +- spec/models/subscription_spec.rb | 2 +- spec/models/user_spec.rb | 2 +- spec/routing/subscriptions_routing_spec.rb | 2 +- spec/tasks/subscription_tasks_spec.rb | 4 ++-- 9 files changed, 21 insertions(+), 21 deletions(-) diff --git a/spec/controllers/subscriptions_controller_spec.rb b/spec/controllers/subscriptions_controller_spec.rb index 48b37a4..82a301d 100644 --- a/spec/controllers/subscriptions_controller_spec.rb +++ b/spec/controllers/subscriptions_controller_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -RSpec.describe SubscriptionsController, :type => :controller do +RSpec.describe SubscriptionsController, type: :controller do context "GET new" do diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index f69667f..9ae1cc3 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -1,9 +1,9 @@ require 'rails_helper' -RSpec.describe ApplicationHelper, :type => :helper do - +RSpec.describe ApplicationHelper, type: :helper do + context "page_title attribute" do - + it "has getter and setter" do helper.page_title = "About Page Title" expect(helper.page_title).to eq("About Page Title") diff --git a/spec/jobs/headline_scraper_job_spec.rb b/spec/jobs/headline_scraper_job_spec.rb index a928e7b..fc17ab3 100644 --- a/spec/jobs/headline_scraper_job_spec.rb +++ b/spec/jobs/headline_scraper_job_spec.rb @@ -1,15 +1,15 @@ require 'rails_helper' -RSpec.describe HeadlineScraperJob, :type => :job do - +RSpec.describe HeadlineScraperJob, type: :job do + it "emails headlines scraped from given URL" do - + url_to_scrape = "https://eliotsykes.github.io/rspec-rails-examples/" recipient_email = "anchor@nightlynews.tld" assert_performed_with( job: HeadlineScraperJob, - args: [{url: url_to_scrape, recipient: recipient_email}], + args: [{url: url_to_scrape, recipient: recipient_email}], queue: 'default' ) do @@ -20,7 +20,7 @@ end open_email recipient_email, with_subject: "Today's Headlines" - + expected_headlines = [ # Original headline edited to prove VCR is serving scraped page. @@ -32,7 +32,7 @@ expected_headlines.each do |expected_headline| expect(current_email).to have_body_text expected_headline end - + end end diff --git a/spec/mailers/news_mailer_spec.rb b/spec/mailers/news_mailer_spec.rb index 03410a1..b3bb4fb 100644 --- a/spec/mailers/news_mailer_spec.rb +++ b/spec/mailers/news_mailer_spec.rb @@ -1,9 +1,9 @@ require "rails_helper" -RSpec.describe NewsMailer, :type => :mailer do - +RSpec.describe NewsMailer, type: :mailer do + describe ".send_headlines(headlines:, to:)" do - + it "has expected subject when there are headlines" do mail = NewsMailer.send_headlines( headlines: ["Scaremongering Headline: Be Fearful!"], @@ -13,7 +13,7 @@ end [nil, []].each do |no_headlines| - + it "has expected subject when there are no headlines '#{no_headlines.inspect}'" do NewsMailer.send_headlines(headlines: no_headlines, to: "newsguy@goodnews.tld") expect(open_last_email).to have_subject "No Headlines Today :-(" @@ -32,7 +32,7 @@ end it "calls the error-sensitive deliver_now!" do - + delivery = double expect(delivery).to receive(:deliver_now!).with(no_args) diff --git a/spec/mailers/subscription_mailer_spec.rb b/spec/mailers/subscription_mailer_spec.rb index 3a2f180..08e916c 100644 --- a/spec/mailers/subscription_mailer_spec.rb +++ b/spec/mailers/subscription_mailer_spec.rb @@ -1,6 +1,6 @@ require "rails_helper" -RSpec.describe SubscriptionMailer, :type => :mailer do +RSpec.describe SubscriptionMailer, type: :mailer do describe ".send_confirmation_request!(subscription)" do diff --git a/spec/models/subscription_spec.rb b/spec/models/subscription_spec.rb index e05d6af..5cc5edd 100644 --- a/spec/models/subscription_spec.rb +++ b/spec/models/subscription_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -RSpec.describe Subscription, :type => :model do +RSpec.describe Subscription, type: :model do context "db" do context "indexes" do diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index d723c1b..5cdeb6d 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -RSpec.describe User, :type => :model do +RSpec.describe User, type: :model do it { should validate_presence_of :email } it { should validate_presence_of :password } diff --git a/spec/routing/subscriptions_routing_spec.rb b/spec/routing/subscriptions_routing_spec.rb index 4860bf2..b373ea1 100644 --- a/spec/routing/subscriptions_routing_spec.rb +++ b/spec/routing/subscriptions_routing_spec.rb @@ -1,6 +1,6 @@ require "rails_helper" -RSpec.describe "routes for Subscriptions", :type => :routing do +RSpec.describe "routes for Subscriptions", type: :routing do context "confirm_subscription route" do diff --git a/spec/tasks/subscription_tasks_spec.rb b/spec/tasks/subscription_tasks_spec.rb index bad307d..6e85176 100644 --- a/spec/tasks/subscription_tasks_spec.rb +++ b/spec/tasks/subscription_tasks_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -RSpec.describe "Subscription tasks", :type => :task do +RSpec.describe "Subscription tasks", type: :task do context "subscription:confirmation_overdue:delete" do @@ -34,7 +34,7 @@ def invoke_task task = Rake::Task["subscription:confirmation_overdue:delete"] - # Ensure task is re-enabled, as rake tasks by default are disabled + # Ensure task is re-enabled, as rake tasks by default are disabled # after running once within a process http://pivotallabs.com/how-i-test-rake-tasks/ task.reenable task.invoke From ac945d932c84ab730b8841ed0ad63f881288b830 Mon Sep 17 00:00:00 2001 From: Vinh Nguyen Date: Wed, 7 Oct 2015 13:48:15 +0700 Subject: [PATCH 2/3] Rspec for pages request --- spec/requests/pages_controller_spec.rb | 29 ++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 spec/requests/pages_controller_spec.rb diff --git a/spec/requests/pages_controller_spec.rb b/spec/requests/pages_controller_spec.rb new file mode 100644 index 0000000..9e743fc --- /dev/null +++ b/spec/requests/pages_controller_spec.rb @@ -0,0 +1,29 @@ +require "rails_helper" + +RSpec.describe "Pages Controller", type: :request do + + context "Index" do + + it "redirects to index page" do + get "/" + expect(response).to have_http_status(:success) + + expect(response).to render_template(:index) + expect(response.body).to include("Welcome to RSpec Rails Examples") + end + + end + + context "Share" do + + it "redirect to share page" do + get "/share" + expect(response).to have_http_status(:success) + + expect(response).to render_template(:share) + expect(response.body).to include("Share Our Stuff!") + end + + end + +end From 221b120fd62c432a60478f96724cbe1e712dbfb8 Mon Sep 17 00:00:00 2001 From: Vinh Nguyen Date: Wed, 7 Oct 2015 15:16:38 +0700 Subject: [PATCH 3/3] Add test for subscription request --- spec/requests/pages_controller_spec.rb | 4 +- .../requests/subscriptions_controller_spec.rb | 40 +++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 spec/requests/subscriptions_controller_spec.rb diff --git a/spec/requests/pages_controller_spec.rb b/spec/requests/pages_controller_spec.rb index 9e743fc..566a468 100644 --- a/spec/requests/pages_controller_spec.rb +++ b/spec/requests/pages_controller_spec.rb @@ -2,7 +2,7 @@ RSpec.describe "Pages Controller", type: :request do - context "Index" do + context "Index request" do it "redirects to index page" do get "/" @@ -14,7 +14,7 @@ end - context "Share" do + context "Share request" do it "redirect to share page" do get "/share" diff --git a/spec/requests/subscriptions_controller_spec.rb b/spec/requests/subscriptions_controller_spec.rb new file mode 100644 index 0000000..39144ea --- /dev/null +++ b/spec/requests/subscriptions_controller_spec.rb @@ -0,0 +1,40 @@ +require "rails_helper" + +RSpec.describe "Subscription Controller", type: :request do + + context "New request" do + + it "returns new subscription page" do + get "/subscriptions/new" + expect(response).to have_http_status(:success) + + expect(response).to render_template(:new) + end + + end + + context "Create request" do + it "returns to pending subscription page" do + params = { subscription: { email: "e@example.tld", start_on: "2014-12-31" } } + post "/subscriptions", params + + expect(response).to have_http_status(:found) + expect(response).to redirect_to(pending_subscriptions_path) + end + end + + context "Confirm request" do + + let(:token) { SecureRandom.hex(32) } + let!(:subscription) { create(:subscription, confirmation_token: token) } + let(:today) { Date.today.to_formatted_s(:long_ordinal) } + + it "returns to subscription confirmation page" do + get "/subscriptions/#{token}/confirm" + + expect(response).to have_http_status(:success) + expect(response.body).to include("Your subscription will start on #{today}, thank you!") + end + end + +end