G5 / jsonapi-resources-matchers

Test matchers for jsonapi-resources
MIT License
31 stars 14 forks source link

feature request: add generator for resource spec file #11

Open Fivell opened 7 years ago

Fivell commented 7 years ago

Hello, generator for resource spec file can be helpful

ramontayag commented 7 years ago

@Fivell I don't have a lot of experience with generators. If you do, feel free to add it :)

BatuhanW commented 7 years ago

We should start from somewhere, so I'm dropping this to here.

require 'rails_helper'

RSpec.describe Resource, type: :resource do
  # let(:user) { create(:user) }
  let(:data) { create() }

  # Some attributes, creatable, updatable fields might be user related
  # subject { described_class.new(data, { current_user: user }) }
  subject { described_class.new(data, {}) }

  it { is_expected.to have_primary_key(:id) }

  context '#attributes' do
    attributes = [:attribute]

    attributes.each do |attribute|
      it { is_expected.to have_attribute(attribute) }
    end
  end

  context '#relationships' do
    context '#one' do
      # it { is_expected.to have_one(:relationship) }
    end

    context '#many' do
      # it { is_expected.to have_many(:relationship) }
    end
  end

  context '#filter' do
    filters = [:field]

    filters.each do |filter|
      it { is_expected.to filter(filter) }
    end
  end

  context '#fields' do
    context '#sortable' do
      sortables = [:field]

      sortables.each do |sortable|
        it { is_expected.to have_sortable_field(sortable) }
      end
    end

    context '#creatable' do
      context '#valid' do
        fields = [:field]

        fields.each do |field|
          # it { is_expected.to have_creatable_field(field) }
        end
      end

      context '#invalid' do
        fields = [:field]

        fields.each do |field|
          # it { is_expected.to_not have_creatable_field(field) }
        end
      end
    end

    context '#updatable' do
      context '#valid' do
        fields = [:field]

        fields.each do |field|
          # it { is_expected.to_not have_updatable_field(field) }
        end
      end

      context '#invalid' do
        fields = [:field]

        fields.each do |field|
          # it { is_expected.to_not have_updatable_field(field) }
        end
      end
    end
  end
end

@Fivell