forked from thoughtbot/factory_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implicitly call FactoryGirl's syntax methods from dynamic attributes
- Loading branch information
1 parent
4682ab0
commit 29a5ab1
Showing
8 changed files
with
96 additions
and
10 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,19 @@ | ||
module FactoryGirl | ||
class NullObject < ::BasicObject | ||
def method_missing(*args) | ||
nil | ||
def initialize(methods_to_respond_to) | ||
@methods_to_respond_to = methods_to_respond_to.map(&:to_s) | ||
end | ||
|
||
def method_missing(name, *args, &block) | ||
if respond_to?(name) | ||
nil | ||
else | ||
super | ||
end | ||
end | ||
|
||
def respond_to?(method, include_private=false) | ||
@methods_to_respond_to.include? method.to_s | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module FactoryGirl | ||
class SyntaxRunner | ||
include Syntax::Methods | ||
end | ||
end |
41 changes: 41 additions & 0 deletions
41
spec/acceptance/syntax_methods_within_dynamic_attributes_spec.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require "spec_helper" | ||
|
||
describe "syntax methods within dynamic attributes" do | ||
before do | ||
define_model("Post", title: :string, user_id: :integer) do | ||
belongs_to :user | ||
|
||
def generate | ||
"generate result" | ||
end | ||
end | ||
define_model("User", email: :string) | ||
|
||
FactoryGirl.define do | ||
sequence(:email_address) {|n| "person-#{n}@example.com" } | ||
|
||
factory :user do | ||
email { generate(:email_address) } | ||
end | ||
|
||
factory :post do | ||
title { generate } | ||
user { build(:user) } | ||
end | ||
end | ||
end | ||
|
||
it "can access syntax methods from dynamic attributes" do | ||
FactoryGirl.build(:user).email.should == "person-1@example.com" | ||
FactoryGirl.attributes_for(:user)[:email].should == "person-2@example.com" | ||
end | ||
|
||
it "can access syntax methods from dynamic attributes" do | ||
FactoryGirl.build(:post).user.should be_instance_of(User) | ||
end | ||
|
||
it "can access methods already existing on the class" do | ||
FactoryGirl.build(:post).title.should == "generate result" | ||
FactoryGirl.attributes_for(:post)[:title].should be_nil | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,22 @@ | ||
require "spec_helper" | ||
|
||
describe FactoryGirl::NullObject do | ||
its(:id) { should be_nil } | ||
its(:age) { should be_nil } | ||
its(:name) { should be_nil } | ||
its(:admin?) { should be_nil } | ||
let(:methods_to_respond_to) { %w[id age name admin?] } | ||
let(:methods_to_not_respond_to) { %w[email date_of_birth title] } | ||
|
||
subject { FactoryGirl::NullObject.new(methods_to_respond_to) } | ||
|
||
it "responds to the given methods" do | ||
methods_to_respond_to.each do |method_name| | ||
subject.__send__(method_name).should be_nil | ||
subject.respond_to?(method_name).should be_true | ||
end | ||
end | ||
|
||
it "does not respond to other methods" do | ||
methods_to_not_respond_to.each do |method_name| | ||
expect { subject.__send__(method_name) }.to raise_error(NoMethodError) | ||
subject.respond_to?(method_name).should be_false | ||
end | ||
end | ||
end |