Top Level Namespace

Defined Under Namespace

Modules: RestPack

Instance Method Summary collapse

Instance Method Details

#is_optional(*params) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/restpack_service/support/matchers.rb', line 12

def is_optional(*params)
  params.each do |param|
    it ":#{param} parameter must be required" do
      expect(subject.class.input_filters.optional_inputs).to include(param)
    end
  end
end

#is_required(*params) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/restpack_service/support/matchers.rb', line 4

def is_required(*params)
  params.each do |param|
    it ":#{param} parameter must be required" do
      expect(subject.class.input_filters.required_inputs).to include(param)
    end
  end
end

#it_acts_as_create_commandObject



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
# File 'lib/restpack_service/support/matchers/create_command.rb', line 1

def it_acts_as_create_command
  let(:response) { subject.class.run(params) }
  let(:resource_plural) { subject.Serializer.plural_key }
  let(:resource_singular) { subject.Serializer.singular_key }

  context "with valid params" do
    context "when creating a single item" do
      let(:item) { build("api_#{resource_singular}") }
      let(:params) { { resource_plural => [item] } }

      it_succeeds "and returns the newly created resource" do
        expect(response.result).to include(resource_plural), "The reponse should include an array of :#{resource_plural}"

        response_items = response.result[resource_plural]
        expect(response_items.length).to eq(1)

        response_item = response_items.first
        model = subject.Model.find(response_item[:id])
        expect(response_item).to eq(subject.Serializer.as_json(model))
      end
    end
  end

  context "when creating multiple items" do
    let(:item1) { build("api_#{resource_singular}".to_sym) }
    let(:item2) { build("api_#{resource_singular}".to_sym) }
    let(:params) { { resource_plural => [item1, item2] } }

    it_succeeds "and returns the newly created resources" do
      expect(response.result).to include(resource_plural), "The reponse should include an array of :#{resource_plural}"

      response_items = response.result[resource_plural]
      expect(response_items.length).to eq(2)

      response_items.each do |response_item|
        model = subject.Model.find(response_item[:id])
        expect(response_item).to eq(subject.Serializer.as_json(model))
      end
    end
  end
end

#it_fails_with(status, &block) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/restpack_service/support/matchers.rb', line 37

def it_fails_with(status, &block)
  it "fails with :#{status}" do
    expect(response.success?).to eq(false)
    expect(response.result).to eq({})
    expect(response.status).to eq(status)
    instance_eval(&block) if block_given?
  end
end

#it_succeeds(message = 'succeeds', &block) ⇒ Object



46
47
48
49
50
51
# File 'lib/restpack_service/support/matchers.rb', line 46

def it_succeeds(message='succeeds', &block)
  it message do
    expect(response.success?).to eq(true)
    instance_eval(&block) if block_given?
  end
end

#service_request_with(param, value, &block) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/restpack_service/support/matchers.rb', line 20

def service_request_with(param, value, &block)
  context "when :#{param} = #{value.inspect}" do
    let(:params) do
      super().merge({ param => value })
    end
    it { yield(response.result) }
  end
end

#service_should_map(param, map) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/restpack_service/support/matchers.rb', line 29

def service_should_map(param, map)
  context param do
    map.each do |value, expected|
      service_request_with(param, value) { |r| r[param].should == expected }
    end
  end
end

#setupObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/restpack_service/support/spec_helper.rb', line 10

def setup
  config = YAML.load_file('./config/database.yml')
  ActiveRecord::Base.establish_connection(ENV['DATABASE_URL'] || config['test'])

  migrations = ActiveRecord::Migrator.up('./db/migrate')

  FactoryGirl.find_definitions

  DatabaseCleaner.strategy = :transaction

  RSpec.configure do |config|
    config.include FactoryGirl::Syntax::Methods

    config.before(:each) do
      DatabaseCleaner.start
    end

    config.after(:each) do
      DatabaseCleaner.clean
    end
  end
end