mardi 16 janvier 2018

Writing tests(rspec) on custom gem(plugin)

The task is to write gem(plugin) which output text to the log when server starts (rails s), I did it, now I can not understand how to start the server in tests, because I checking:

if defined? (Rails :: Server)

# seated_plugin(lib/seated_plugin.rb)

    module SeatedPlugin
      class << self
        attr_accessor :configuration
      end
      def self.configure
        self.configuration ||= Configuration.new
        yield(configuration)
        if defined?(Rails::Server)
          print configuration.text
        end
      end

      class Configuration
        attr_accessor :text

        def initialize
          @text = '░░░░██▄
    ░░░██▀░░░░▐
    ▌░███▄░░░░▐
    ▌▐███░▀▄███▄▄▄██▄▄
    ▌█████▌░░▌░░░░░░▌
    ▌▀▀▀▌▐█░░▌░░░░░░▌
    ▌▀▀▀▌▐█░░▌░░░░░░▌
    ▌░░░▌░█▄▌░░░░░░░▌'
        end
      end
    end
   # install generator (lib/generators/seated_plugin/install_generator.rb)
        require 'rails/generators/base'
        module SeatedPlugin
          class InstallGenerator < Rails::Generators::Base
            source_root File.expand_path('../templates', __FILE__)
            def copy_initializer
              template "seated_plugin.rb", "config/initializers/seated_plugin.rb"
              puts "Install complete!"
            end
          end
        end
   # Tests
   # (spec/install_generator_spec.rb)

    require "generator_spec"
    require 'spec_helper'
    require 'generators/seated_plugin/install_generator.rb'
    describe SeatedPlugin::InstallGenerator, type: :generator do
      destination(File.expand_path("../../tmp", __FILE__))

        before(:all) do
          prepare_destination
        end

        it "creates a install initializer" do
          run_generator
          assert_file "config/initializers/seated_plugin.rb", "SeatedPlugin.config do |config|
    # Set this options to what makes sense for you
    # config.text = 'different_text'
    end"
        end
    end
# (spec/module_test_spec.rb)

    require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
    require 'seated_plugin'
    describe SeatedPlugin do
      it 'test config gem to default parameter text' do
      SeatedPlugin.configure do |config|
        # Set this options to what makes sense for you
        # config.text = 'different_text'
      end
      expect(SeatedPlugin.configuration.text).to eq('░░░░██▄
    ░░░██▀░░░░▐
    ▌░███▄░░░░▐
    ▌▐███░▀▄███▄▄▄██▄▄
    ▌█████▌░░▌░░░░░░▌
    ▌▀▀▀▌▐█░░▌░░░░░░▌
    ▌▀▀▀▌▐█░░▌░░░░░░▌
    ▌░░░▌░█▄▌░░░░░░░▌')

    end
      it 'test config gem to custom parameter text' do
        SeatedPlugin.configure do |config|
          config.text = 'aaa'
        end
        expect(SeatedPlugin.configuration.text).to eq('aaa')

      end
      it 'test config gem to custom parameter text' do
        SeatedPlugin.configure do |config|
          # config.text = 'aaa'
          expect { config.text = 'aaa' }.to output("aaa").to_stdout
        end
      end
    end

#ERROR #Failure/Error: expect { config.text = 'aaa' }.to output("aaa").to_stdout #expected block to output "aaa" to stdout, but output nothing

Aucun commentaire:

Enregistrer un commentaire