From 96c6b74b6028bac70d9c9a5ee5b2ec7ee1a051fe Mon Sep 17 00:00:00 2001 From: Aotokitsuruya Date: Thu, 9 Nov 2023 22:18:47 +0800 Subject: [PATCH] Refactor letsencrypt generator spec --- .../lets_encrypt/install_generator_spec.rb | 15 +++++++++------ .../lets_encrypt/register_generator_spec.rb | 18 ++++++++++-------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/spec/letsencrypt/generators/lets_encrypt/install_generator_spec.rb b/spec/letsencrypt/generators/lets_encrypt/install_generator_spec.rb index 26e4594..c8e4b29 100644 --- a/spec/letsencrypt/generators/lets_encrypt/install_generator_spec.rb +++ b/spec/letsencrypt/generators/lets_encrypt/install_generator_spec.rb @@ -5,18 +5,21 @@ require 'generators/lets_encrypt/install_generator' RSpec.describe LetsEncrypt::Generators::InstallGenerator do - before(:all) do - @dummy_class = Class.new(Rails::Generators::TestCase) do + let(:klass) do + Class.new(Rails::Generators::TestCase) do tests LetsEncrypt::Generators::InstallGenerator destination Rails.root.join('tmp') end + end + + let(:generator) { klass.new(:fake_test_case) } - @generator = @dummy_class.new(:fake_test_case) - @generator.send(:prepare_destination) - @generator.run_generator + before do + generator.send(:prepare_destination) + generator.run_generator end it do - @generator.assert_migration 'db/migrate/create_letsencrypt_certificates.rb', /def change/ + generator.assert_migration 'db/migrate/create_letsencrypt_certificates.rb', /def change/ end end diff --git a/spec/letsencrypt/generators/lets_encrypt/register_generator_spec.rb b/spec/letsencrypt/generators/lets_encrypt/register_generator_spec.rb index c06db28..273df57 100644 --- a/spec/letsencrypt/generators/lets_encrypt/register_generator_spec.rb +++ b/spec/letsencrypt/generators/lets_encrypt/register_generator_spec.rb @@ -5,15 +5,16 @@ require 'generators/lets_encrypt/register_generator' RSpec.describe LetsEncrypt::Generators::RegisterGenerator do - before(:all) do - @dummy_class = Class.new(Rails::Generators::TestCase) do + let(:klass) do + Class.new(Rails::Generators::TestCase) do tests LetsEncrypt::Generators::RegisterGenerator destination Rails.root.join('tmp') end - @generator = @dummy_class.new(:fake_test_case) end - it do + let(:generator) { klass.new(:fake_test_case) } + + before do answers = [ '', # In production '', # File path @@ -21,10 +22,11 @@ 'example@example.com' # E-Mail ] allow(Thor::LineEditor).to receive(:readline) { answers.shift.dup || 'N' } + allow(LetsEncrypt).to receive(:register).and_return(true) - expect(LetsEncrypt).to receive(:register).and_return(true) - - @generator.send(:prepare_destination) - @generator.run_generator + generator.send(:prepare_destination) + generator.run_generator end + + it { expect(LetsEncrypt).to have_received(:register).with('example@example.com') } end