Skip to content

Instantly share code, notes, and snippets.

@poppen
Created September 8, 2011 01:19
Show Gist options
  • Select an option

  • Save poppen/1202364 to your computer and use it in GitHub Desktop.

Select an option

Save poppen/1202364 to your computer and use it in GitHub Desktop.
# encoding: utf-8
require 'rubygems'
require 'spork'
Spork.prefork do
# Loading more in this block will cause your tests to run faster. However,
# if you change any configuration or code from libraries loaded here, you'll
# need to restart spork for it take effect.
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'capybara/rails'
require 'factory_girl'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
# == Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
config.mock_with :rspec
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
#config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
end
class SpecCreateDeleteCheck
def initialize(check_class, hash)
@check_class = check_class
@check_keys = hash[:check_keys] || []
@relation_strings = hash[:relation_strings] || []
@one_relation_strings = hash[:one_relation_strings] || []
@attributes = hash[:attributes] || {}
@method_name = ncm(hash[:method_name], '') { |o| o.to_s }
@one_method_name = ncm(hash[:one_method_name], '') { |o| o.to_s }
end
attr_reader :check_class, :check_keys, :relation_strings, :one_relation_strings
attr_reader :attributes, :method_name, :one_method_name
def make_object_with_relations(relations)
an_object = @check_class.create @attributes
@relation_strings.each do |ww|
ww.to_s.constantize.instance_method(@method_name).bind(relations[ww]).call << an_object
end
@one_relation_strings.each do |ww|
ww.to_s.constantize.instance_method(@one_method_name + '=').bind(relations[ww]).call(an_object)
end
an_object
end
end
def create_delete_check(check_class, hash)
s = SpecCreateDeleteCheck.new(check_class, hash)
create_check s
delete_check s
end
def create_check(s)
context "作成するとき" do
before(:each) do
@valid_attributes = s.attributes.dup
end
it "正しいアトリビュートに対して作成が成功すること" do
s.check_class.create!(@valid_attributes)
end
s.check_keys.each do |key|
it "#{key} 属性が設定されていない場合にバリデーションに失敗すること" do
@valid_attributes.delete key
s.check_class.new(@valid_attributes).should_not be_valid
end
end
s.relation_strings.each do |w|
it "#{w} に所属するオブジェクトが一つ増えること" do
r = @relations[w]
method = w.to_s.constantize.instance_method(s.method_name).bind(r)
lambda { s.make_object_with_relations(@relations) }.should change(method.call, :count).by(1)
end
end
s.one_relation_strings.each do |w|
it "#{w} から #{s.one_method_name} として参照できること" do
r = @relations[w]
method = w.to_s.constantize.instance_method(s.one_method_name).bind(r)
child = s.make_object_with_relations(@relations)
method.call.should == child
end
end
end
end
def delete_check(s)
if s.relation_strings.length > 0 || s.one_relation_strings.length > 0
context "削除するとき" do
before(:each) do
@an_object = s.make_object_with_relations(@relations)
end
s.relation_strings.each do |w|
it "#{ w } に所属するオブジェクトが一つ減ること" do
method = w.to_s.constantize.instance_method(s.method_name).bind(@relations[w])
lambda {
@an_object.destroy
@an_object.should_not have(1).errors
}.should change(method.call, :count).by(-1)
end
end
s.one_relation_strings.each do |w|
it "#{w}.#{s.one_method_name}が nil になること" do
method = w.to_s.constantize.instance_method(s.one_method_name).bind(@relations[w])
end
end
end
end
end
ActiveSupport::Dependencies.clear
ActiveRecord::Base.instantiate_observers
def set_current_employee(employee)
controller.stub!(:current_employee).and_return(employee)
end
end
Spork.each_run do
# This code will be run each time you run your specs.
FactoryGirl.factories.clear
FactoryGirl.sequences.clear
FactoryGirl.traits.clear
Dir[Rails.root.join("spec/factories/**/*.rb")].each{|f| load f}
load "#{Rails.root}/config/routes.rb"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment