Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 58 lines (48 sloc) 1.62 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
require 'spec_helper'
require 'fixtures/user'

describe CsvShaper::Row do
  let(:user) { User.new(name: 'Paul', age: 27, gender: 'Male') }
  
  it "should assign a model" do
    row = CsvShaper::Row.new user do |csv, user|
      # ...
    end

    row.model.should eq user
  end
  
  it "should assign a model's attributes" do
    row = CsvShaper::Row.new(user, :name, :age)
    row.cells.should eq({ name: 'Paul', age: 27 })
  end
  
  it "should yield to a block" do
    CsvShaper::Row.new { |csv|
      csv.should be_kind_of(CsvShaper::Row)
    }
  end
  
  describe "cells" do
    it "should send parse an attribute of the model" do
      row = CsvShaper::Row.new(user, :gender)
      row.cell :name
      row.cells.should eq({ name: 'Paul', gender: 'Male' })
    end
    
    it "should send assign an unrelated value" do
      row = CsvShaper::Row.new(user, :gender)
      row.cell :foo, 'bar'
      row.cells.should eq({ foo: 'bar', gender: 'Male' })
    end
    
    it "ignore nil values passed" do
      row = CsvShaper::Row.new(user, :gender)
      row.cell :foo, nil
      row.cells.should eq({ foo: nil, gender: 'Male' })
    end
    
    it "should not send column to model if two args are passed" do
      row = CsvShaper::Row.new(user, :gender)
      row.cell :name, 'Another name'
      row.cells.should eq({ name: 'Another name', gender: 'Male' })
    end
    
    it "should raise an exception of the model does not respond to column, and no value is passed" do
      row = CsvShaper::Row.new(user, :gender)
      expect {
        row.cell :foo
      }.to raise_error(ArgumentError)
    end
  end
end
Something went wrong with that request. Please try again.