Sha256: 87be7e8f5670992cdbdf1517e985ec34d46ce3ffad4add1364571c4936f34ae6

Contents?: true

Size: 1.97 KB

Versions: 17

Compression:

Stored size: 1.97 KB

Contents

require 'spec_helper'

describe Society::Formatter::Report::Json do

  before do # don't actually change the filesystem
    allow(FileUtils).to receive(:mkpath)
    allow(File).to receive(:open)
  end

  let(:json_data) { {foo: 'bar'}.to_json }

  describe "(private) #prepare_output_directory" do
    context "with output path specified" do

      let(:output_path) { File.join(%w[blah mah_data.json]) }
      let(:report) do
        Society::Formatter::Report::Json.new(
          json_data: json_data,
          output_path: output_path
        )
      end

      it "creates the output directory" do
        output_directory_matcher = /blah/
        expect(FileUtils).to receive(:mkpath).with(output_directory_matcher)
        report.send(:prepare_output_directory)
      end
    end

    context "with no output path specified" do

      let(:report) do
        Society::Formatter::Report::Json.new(
          json_data: json_data
        )
      end

      it "raises an error since no output was specified" do
        expect{report.send(:prepare_output_directory)}.to raise_error(/No output/)
      end
    end
  end

  describe "#write" do

    context "with output path specified" do
      let(:output_path) { File.join(%w[blah mah_data.json]) }
      let(:report) do
        Society::Formatter::Report::Json.new(
          json_data: json_data,
          output_path: output_path
        )
      end


      let(:open_file) { double }

      it "writes the json data" do
        expect(File).to receive(:open).with(output_path, 'w') do |path, mode, &block|
          block.yield open_file
        end
        expect(open_file).to receive(:write).with(json_data)
        report.write
      end
    end
    context "with no output path specified" do

      let(:report) do
        Society::Formatter::Report::Json.new(
          json_data: json_data
        )
      end

      it "write json to stdout" do
        expect{report.write}.to output("{\"foo\":\"bar\"}\n").to_stdout
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
society-2.0.1 spec/formatter/report/json_spec.rb
society-2.0 spec/formatter/report/json_spec.rb
society-1.7 spec/formatter/report/json_spec.rb
society-1.6 spec/formatter/report/json_spec.rb
society-1.5.2 spec/formatter/report/json_spec.rb
society-1.5.1 spec/formatter/report/json_spec.rb
society-1.5.0 spec/formatter/report/json_spec.rb
society-1.4.2 spec/formatter/report/json_spec.rb
society-1.4.1 spec/formatter/report/json_spec.rb
society-1.4.0 spec/formatter/report/json_spec.rb
society-1.3.0 spec/formatter/report/json_spec.rb
society-1.2.3 spec/formatter/report/json_spec.rb
society-1.2.2 spec/formatter/report/json_spec.rb
society-1.2.1 spec/formatter/report/json_spec.rb
society-1.2.0 spec/formatter/report/json_spec.rb
society-1.1.1 spec/formatter/report/json_spec.rb
society-1.1.0 spec/formatter/report/json_spec.rb