Created
March 12, 2017 16:44
-
-
Save EastResident/932910b043c1a001eb224e3bcc7f41dc to your computer and use it in GitHub Desktop.
モデル間のリレーションSpecを書きたくないので自動生成してみる ref: http://qiita.com/EastResident/items/e97632074f50d2541547
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Customer < ApplicationRecord | |
has_many :orders, dependent: :destroy | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RSpec.describe Customer, type: :model, relation_specs: true do | |
end | |
# 下記のコードが実行されるのと同じ | |
# RSpec.describe Customer, type: :model do | |
# describe 'association' do | |
# describe 'has_many' do | |
# it { is_expected.to have_many(:orders).dependent(:destroy) } | |
# end | |
# end | |
# end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'erb' | |
module AutoSpec | |
module ModelSpec | |
def exe_relation_specs | |
@klass = Object.const_get(to_s.split('::').last.split('_').first) | |
temp = open("#{File.expand_path('..', __FILE__)}/templates/relations.erb") do |f| | |
ERB.new(f.read).result(binding) | |
end | |
instance_eval temp | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Order < ApplicationRecord | |
belongs_to :customer | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RSpec.describe Order, type: :model, relation_specs: true do | |
end | |
# 下記のコードが実行されるのと同じ | |
# RSpec.describe Order, type: :model do | |
# describe 'association' do | |
# describe 'belongs_to' do | |
# it { is_expected.to belong_to(:customer) } | |
# end | |
# end | |
# end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
shared_context :relation_specs, relation_specs: true do | |
extend AutoSpec::ModelSpec | |
exe_relation_specs | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% if @klass.reflect_on_all_associations %>describe 'relation' do | |
<% if (ass = @klass.reflect_on_all_associations(:has_one)).present? %>context 'has_one' do | |
<% ass.each do |as| %> | |
it { is_expected.to have_one(:<%= as.name %>)<%= as.options.reduce([]) { |a, e| a << (%w(foreign_key primary_key).include?(e[0].to_s) ? "\.with_#{e[0]}(:#{e[1]})" : "\.#{e[0]}(:#{e[1]})") }.join %> }<% end %> | |
end<% end %> | |
<% if (ass = @klass.reflect_on_all_associations(:has_many)).present? %>context 'has_many' do | |
<% ass.each do |as| %> | |
it { is_expected.to have_many(:<%= as.name %>)<%= as.options.reduce([]) { |a, e| a << (%w(foreign_key primary_key).include?(e[0].to_s) ? "\.with_#{e[0]}(:#{e[1]})" : "\.#{e[0]}(:#{e[1]})") }.join %> }<% end %> | |
end<% end %> | |
<% if (ass = @klass.reflect_on_all_associations(:belongs_to)).present? %>context 'belongs_to' do | |
<% ass.each do |as| %> | |
it { is_expected.to belong_to(:<%= as.name %>)<%= as.options.reduce([]) { |a, e| a << (%w(foreign_key primary_key).include?(e[0].to_s) ? "\.with_#{e[0]}(:#{e[1]})" : "\.#{e[0]}(:#{e[1]})") }.join %> }<% end %> | |
end<% end %> | |
end<% end %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment