require 'rails_helper'

describe TargetFile::FileWithoutBuilder do
  describe ".builder_class" do
    it "returns the basic CsvBuilder class when no specific builder can be found" do
      described_class.builder_class.should eq CsvBuilder
    end
  end

  describe "#builder" do
    subject { described_class.new.builder }
    it "works with not implemented defaults" do
      subject.should be_a CsvBuilder
      subject.options.should eq Hash.new
    end

    it "can implement custop options" do
      allow_any_instance_of( described_class ).to receive(:builder_options).and_return(has_custom_options: 8)
      subject.options.should eq(has_custom_options: 8)
    end

    it "implements default data behaviour expecting a good iteration resource not being enumerable, but responding to id" do
      allow_any_instance_of( described_class ).to receive(:resource).and_return(double(id: 8))
      subject.data.should eq "8\n"
    end

    it "implements default data behaviour expecting a good iteration resource" do
      allow_any_instance_of( described_class ).to receive(:resource).and_return([double(id: 8), double(id: 9)])
      subject.data.should eq "8\n9\n"
    end

    it "implements default data behaviour expecting a good iteration resource not being enumerable, but responding to id" do
      allow_any_instance_of( described_class ).to receive(:resource).and_return(double(id: 8, to_csv: "custom;implemented;csv"))
      subject.data.should eq "custom;implemented;csv"
    end

    it "uses the predefined custom builder without stubs" do
      subject.data.should eq "my;custom;csv\n1;2;3\n"
    end
  end
end
