mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
rr => mocha
This commit is contained in:
parent
be14aa49a2
commit
554e6ab378
27 changed files with 264 additions and 277 deletions
3
Gemfile
3
Gemfile
|
@ -5,7 +5,8 @@ gem "rspec", "~> 2.0"
|
|||
gem "rcov"
|
||||
gem "activerecord", :require => false
|
||||
gem "activesupport", :require => false
|
||||
gem "rr"
|
||||
gem "mocha"
|
||||
gem "bourne"
|
||||
gem "sqlite3-ruby", :require => false
|
||||
gem "appraisal", "~> 0.3.5"
|
||||
gem "yard"
|
||||
|
|
|
@ -22,6 +22,8 @@ GEM
|
|||
cucumber (>= 0.10.5)
|
||||
rspec (>= 2.6.0)
|
||||
bluecloth (2.0.9)
|
||||
bourne (1.0)
|
||||
mocha (= 0.9.8)
|
||||
builder (2.1.2)
|
||||
childprocess (0.1.9)
|
||||
ffi (~> 1.0.6)
|
||||
|
@ -37,9 +39,10 @@ GEM
|
|||
json (>= 1.4.6)
|
||||
i18n (0.4.2)
|
||||
json (1.5.3)
|
||||
mocha (0.9.8)
|
||||
rake
|
||||
rake (0.9.2)
|
||||
rcov (0.9.9)
|
||||
rr (1.0.2)
|
||||
rspec (2.6.0)
|
||||
rspec-core (~> 2.6.0)
|
||||
rspec-expectations (~> 2.6.0)
|
||||
|
@ -61,10 +64,11 @@ DEPENDENCIES
|
|||
activesupport
|
||||
appraisal (~> 0.3.5)
|
||||
bluecloth
|
||||
bourne
|
||||
cucumber (~> 1.0.0)
|
||||
mocha
|
||||
rake
|
||||
rcov
|
||||
rr
|
||||
rspec (~> 2.0)
|
||||
sqlite3-ruby
|
||||
yard
|
||||
|
|
|
@ -22,7 +22,8 @@ Gem::Specification.new do |s|
|
|||
s.add_development_dependency('cucumber')
|
||||
s.add_development_dependency('activerecord', '~>2.3.5')
|
||||
s.add_development_dependency('activerecord', '~>3.0.0.beta3')
|
||||
s.add_development_dependency('rr')
|
||||
s.add_development_dependency('mocha')
|
||||
s.add_development_dependency('bourne')
|
||||
s.add_development_dependency('sqlite3')
|
||||
s.add_development_dependency('aruba')
|
||||
|
||||
|
|
|
@ -2,15 +2,17 @@
|
|||
|
||||
source "http://rubygems.org"
|
||||
|
||||
gem "test-unit", "~> 2.3.0", :platforms=>[:mri_19]
|
||||
gem "rr"
|
||||
gem "activerecord", "~> 2.1"
|
||||
gem "rake"
|
||||
gem "sqlite3-ruby", :require=>false
|
||||
gem "bluecloth"
|
||||
gem "rspec", "~> 2.0"
|
||||
gem "yard"
|
||||
gem "rcov"
|
||||
gem "cucumber", "~> 1.0.0"
|
||||
gem "rake"
|
||||
gem "rspec", "~> 2.0"
|
||||
gem "rcov"
|
||||
gem "activerecord", "~> 2.1"
|
||||
gem "activesupport", :require=>false
|
||||
gem "mocha"
|
||||
gem "bourne"
|
||||
gem "sqlite3-ruby", :require=>false
|
||||
gem "appraisal", "~> 0.3.5"
|
||||
gem "yard"
|
||||
gem "bluecloth"
|
||||
gem "test-unit", "~> 2.3.0", :platforms=>[:mri_19]
|
||||
|
||||
|
|
|
@ -13,6 +13,8 @@ GEM
|
|||
cucumber (>= 0.10.5)
|
||||
rspec (>= 2.6.0)
|
||||
bluecloth (2.1.0)
|
||||
bourne (1.0)
|
||||
mocha (= 0.9.8)
|
||||
builder (3.0.0)
|
||||
childprocess (0.1.9)
|
||||
ffi (~> 1.0.6)
|
||||
|
@ -27,9 +29,10 @@ GEM
|
|||
gherkin (2.4.1)
|
||||
json (>= 1.4.6)
|
||||
json (1.5.3)
|
||||
mocha (0.9.8)
|
||||
rake
|
||||
rake (0.9.2)
|
||||
rcov (0.9.9)
|
||||
rr (1.0.2)
|
||||
rspec (2.6.0)
|
||||
rspec-core (~> 2.6.0)
|
||||
rspec-expectations (~> 2.6.0)
|
||||
|
@ -50,12 +53,14 @@ PLATFORMS
|
|||
|
||||
DEPENDENCIES
|
||||
activerecord (~> 2.1)
|
||||
activesupport
|
||||
appraisal (~> 0.3.5)
|
||||
bluecloth
|
||||
bourne
|
||||
cucumber (~> 1.0.0)
|
||||
mocha
|
||||
rake
|
||||
rcov
|
||||
rr
|
||||
rspec (~> 2.0)
|
||||
sqlite3-ruby
|
||||
test-unit (~> 2.3.0)
|
||||
|
|
|
@ -2,14 +2,16 @@
|
|||
|
||||
source "http://rubygems.org"
|
||||
|
||||
gem "rr"
|
||||
gem "activerecord", "~> 2.3"
|
||||
gem "rake"
|
||||
gem "sqlite3-ruby", :require=>false
|
||||
gem "bluecloth"
|
||||
gem "rspec", "~> 2.0"
|
||||
gem "yard"
|
||||
gem "rcov"
|
||||
gem "cucumber", "~> 1.0.0"
|
||||
gem "rake"
|
||||
gem "rspec", "~> 2.0"
|
||||
gem "rcov"
|
||||
gem "activerecord", "~> 2.3"
|
||||
gem "activesupport", :require=>false
|
||||
gem "mocha"
|
||||
gem "bourne"
|
||||
gem "sqlite3-ruby", :require=>false
|
||||
gem "appraisal", "~> 0.3.5"
|
||||
gem "yard"
|
||||
gem "bluecloth"
|
||||
|
||||
|
|
|
@ -13,6 +13,8 @@ GEM
|
|||
cucumber (>= 0.10.5)
|
||||
rspec (>= 2.6.0)
|
||||
bluecloth (2.1.0)
|
||||
bourne (1.0)
|
||||
mocha (= 0.9.8)
|
||||
builder (3.0.0)
|
||||
childprocess (0.1.9)
|
||||
ffi (~> 1.0.6)
|
||||
|
@ -27,9 +29,10 @@ GEM
|
|||
gherkin (2.4.1)
|
||||
json (>= 1.4.6)
|
||||
json (1.5.3)
|
||||
mocha (0.9.8)
|
||||
rake
|
||||
rake (0.9.2)
|
||||
rcov (0.9.9)
|
||||
rr (1.0.2)
|
||||
rspec (2.6.0)
|
||||
rspec-core (~> 2.6.0)
|
||||
rspec-expectations (~> 2.6.0)
|
||||
|
@ -49,12 +52,14 @@ PLATFORMS
|
|||
|
||||
DEPENDENCIES
|
||||
activerecord (~> 2.3)
|
||||
activesupport
|
||||
appraisal (~> 0.3.5)
|
||||
bluecloth
|
||||
bourne
|
||||
cucumber (~> 1.0.0)
|
||||
mocha
|
||||
rake
|
||||
rcov
|
||||
rr
|
||||
rspec (~> 2.0)
|
||||
sqlite3-ruby
|
||||
yard
|
||||
|
|
|
@ -2,14 +2,16 @@
|
|||
|
||||
source "http://rubygems.org"
|
||||
|
||||
gem "rr"
|
||||
gem "activerecord", "~> 3.0"
|
||||
gem "rake"
|
||||
gem "sqlite3-ruby", :require=>false
|
||||
gem "bluecloth"
|
||||
gem "rspec", "~> 2.0"
|
||||
gem "yard"
|
||||
gem "rcov"
|
||||
gem "cucumber", "~> 1.0.0"
|
||||
gem "rake"
|
||||
gem "rspec", "~> 2.0"
|
||||
gem "rcov"
|
||||
gem "activerecord", "~> 3.0"
|
||||
gem "activesupport", :require=>false
|
||||
gem "mocha"
|
||||
gem "bourne"
|
||||
gem "sqlite3-ruby", :require=>false
|
||||
gem "appraisal", "~> 0.3.5"
|
||||
gem "yard"
|
||||
gem "bluecloth"
|
||||
|
||||
|
|
|
@ -21,6 +21,8 @@ GEM
|
|||
cucumber (>= 0.10.5)
|
||||
rspec (>= 2.6.0)
|
||||
bluecloth (2.1.0)
|
||||
bourne (1.0)
|
||||
mocha (= 0.9.8)
|
||||
builder (2.1.2)
|
||||
childprocess (0.1.9)
|
||||
ffi (~> 1.0.6)
|
||||
|
@ -36,9 +38,10 @@ GEM
|
|||
json (>= 1.4.6)
|
||||
i18n (0.5.0)
|
||||
json (1.5.3)
|
||||
mocha (0.9.8)
|
||||
rake
|
||||
rake (0.9.2)
|
||||
rcov (0.9.9)
|
||||
rr (1.0.2)
|
||||
rspec (2.6.0)
|
||||
rspec-core (~> 2.6.0)
|
||||
rspec-expectations (~> 2.6.0)
|
||||
|
@ -59,12 +62,14 @@ PLATFORMS
|
|||
|
||||
DEPENDENCIES
|
||||
activerecord (~> 3.0)
|
||||
activesupport
|
||||
appraisal (~> 0.3.5)
|
||||
bluecloth
|
||||
bourne
|
||||
cucumber (~> 1.0.0)
|
||||
mocha
|
||||
rake
|
||||
rcov
|
||||
rr
|
||||
rspec (~> 2.0)
|
||||
sqlite3-ruby
|
||||
yard
|
||||
|
|
|
@ -64,17 +64,17 @@ end
|
|||
describe "defining a factory" do
|
||||
before do
|
||||
@name = :user
|
||||
@factory = "factory"
|
||||
@factory = stub("factory", :names => [@name])
|
||||
@proxy = "proxy"
|
||||
stub(@factory).names { [@name] }
|
||||
@options = { :class => 'magic' }
|
||||
stub(FactoryGirl::Factory).new { @factory }
|
||||
stub(FactoryGirl::DefinitionProxy).new { @proxy }
|
||||
FactoryGirl::Factory.stubs(:new => @factory)
|
||||
FactoryGirl::DefinitionProxy.stubs(:new => @proxy)
|
||||
end
|
||||
|
||||
it "should create a new factory using the specified name and options" do
|
||||
mock(FactoryGirl::Factory).new(@name, @options) { @factory }
|
||||
FactoryGirl::Factory.stubs(:new => @factory)
|
||||
Factory.define(@name, @options) {|f| }
|
||||
FactoryGirl::Factory.should have_received(:new).with(@name, @options)
|
||||
end
|
||||
|
||||
it "should pass the factory do the block" do
|
||||
|
@ -100,35 +100,39 @@ describe "after defining a factory" do
|
|||
end
|
||||
|
||||
it "should use Proxy::AttributesFor for Factory.attributes_for" do
|
||||
mock(@factory).run(FactoryGirl::Proxy::AttributesFor, :attr => 'value') { 'result' }
|
||||
@factory.stubs(:run => "result")
|
||||
Factory.attributes_for(@name, :attr => 'value').should == 'result'
|
||||
@factory.should have_received(:run).with(FactoryGirl::Proxy::AttributesFor, :attr => 'value')
|
||||
end
|
||||
|
||||
it "should use Proxy::Build for Factory.build" do
|
||||
mock(@factory).run(FactoryGirl::Proxy::Build, :attr => 'value') { 'result' }
|
||||
@factory.stubs(:run => "result")
|
||||
Factory.build(@name, :attr => 'value').should == 'result'
|
||||
@factory.should have_received(:run).with(FactoryGirl::Proxy::Build, :attr => 'value')
|
||||
end
|
||||
|
||||
it "should use Proxy::Create for Factory.create" do
|
||||
mock(@factory).run(FactoryGirl::Proxy::Create, :attr => 'value') { 'result' }
|
||||
@factory.stubs(:run => "result")
|
||||
Factory.create(@name, :attr => 'value').should == 'result'
|
||||
@factory.should have_received(:run).with(FactoryGirl::Proxy::Create, :attr => 'value')
|
||||
end
|
||||
|
||||
it "should use Proxy::Stub for Factory.stub" do
|
||||
mock(@factory).run(FactoryGirl::Proxy::Stub, :attr => 'value') { 'result' }
|
||||
@factory.stubs(:run => "result")
|
||||
Factory.stub(@name, :attr => 'value').should == 'result'
|
||||
@factory.should have_received(:run).with(FactoryGirl::Proxy::Stub, :attr => 'value')
|
||||
end
|
||||
|
||||
it "should use default strategy option as Factory.default_strategy" do
|
||||
stub(@factory).default_strategy { :create }
|
||||
mock(@factory).run(FactoryGirl::Proxy::Create, :attr => 'value') { 'result' }
|
||||
@factory.stubs(:default_strategy => :create, :run => "result")
|
||||
Factory.default_strategy(@name, :attr => 'value').should == 'result'
|
||||
@factory.should have_received(:run).with(FactoryGirl::Proxy::Create, :attr => 'value')
|
||||
end
|
||||
|
||||
it "should use the default strategy for the global Factory method" do
|
||||
stub(@factory).default_strategy { :create }
|
||||
mock(@factory).run(FactoryGirl::Proxy::Create, :attr => 'value') { 'result' }
|
||||
@factory.stubs(:default_strategy => :create, :run => "result")
|
||||
Factory(@name, :attr => 'value').should == 'result'
|
||||
@factory.should have_received(:run).with(FactoryGirl::Proxy::Create, :attr => 'value')
|
||||
end
|
||||
|
||||
[:build, :create, :attributes_for, :stub].each do |method|
|
||||
|
@ -137,7 +141,7 @@ describe "after defining a factory" do
|
|||
end
|
||||
|
||||
it "should recognize either 'name' or :name for Factory.#{method}" do
|
||||
stub(@factory).run
|
||||
@factory.stubs(:run)
|
||||
lambda { Factory.send(method, @name.to_s) }.should_not raise_error
|
||||
lambda { Factory.send(method, @name.to_sym) }.should_not raise_error
|
||||
end
|
||||
|
@ -146,26 +150,22 @@ end
|
|||
|
||||
describe "defining a sequence" do
|
||||
before do
|
||||
@name = :count
|
||||
@sequence = FactoryGirl::Sequence.new(@name) {}
|
||||
stub(FactoryGirl::Sequence).new { @sequence }
|
||||
@name = :count
|
||||
end
|
||||
|
||||
it "should create a new sequence" do
|
||||
mock(FactoryGirl::Sequence).new(@name, 1) { @sequence }
|
||||
Factory.sequence(@name)
|
||||
Factory.next(@name).should == 1
|
||||
end
|
||||
|
||||
it "should use the supplied block as the sequence generator" do
|
||||
stub(FactoryGirl::Sequence).new { @sequence }.yields(1)
|
||||
yielded = false
|
||||
Factory.sequence(@name) {|n| yielded = true }
|
||||
(yielded).should be
|
||||
Factory.sequence(@name) {|n| "user-#{n}" }
|
||||
Factory.next(@name).should == "user-1"
|
||||
end
|
||||
|
||||
it "should use the supplied start_value as the sequence start_value" do
|
||||
mock(FactoryGirl::Sequence).new(@name, "A") { @sequence }
|
||||
Factory.sequence(@name, "A")
|
||||
Factory.next(@name).should == "A"
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -175,16 +175,15 @@ describe "after defining a sequence" do
|
|||
@sequence = FactoryGirl::Sequence.new(@name) {}
|
||||
@value = '1 2 5'
|
||||
|
||||
stub(@sequence).next { @value }
|
||||
stub(FactoryGirl::Sequence).new { @sequence }
|
||||
@sequence.stubs(:next => @value)
|
||||
FactoryGirl::Sequence.stubs(:new => @sequence)
|
||||
|
||||
Factory.sequence(@name) {}
|
||||
end
|
||||
|
||||
it "should call next on the sequence when sent next" do
|
||||
mock(@sequence).next
|
||||
|
||||
Factory.next(@name)
|
||||
@sequence.should have_received(:next)
|
||||
end
|
||||
|
||||
it "should return the value from the sequence" do
|
||||
|
|
|
@ -21,10 +21,9 @@ describe FactoryGirl::Attribute::Association do
|
|||
end
|
||||
|
||||
it "should tell the proxy to associate when being added to a proxy" do
|
||||
proxy = "proxy"
|
||||
stub(proxy).associate
|
||||
proxy = stub("proxy", :associate => nil)
|
||||
@attr.add_to(proxy)
|
||||
proxy.should have_received.associate(@name, @factory, @overrides)
|
||||
proxy.should have_received(:associate).with(@name, @factory, @overrides)
|
||||
end
|
||||
|
||||
it "should convert names to symbols" do
|
||||
|
|
|
@ -12,9 +12,9 @@ describe FactoryGirl::Attribute::Callback do
|
|||
end
|
||||
|
||||
it "should set its callback on a proxy" do
|
||||
@proxy = "proxy"
|
||||
mock(@proxy).add_callback(@name, @block)
|
||||
@attr.add_to(@proxy)
|
||||
proxy = stub("proxy", :add_callback => true)
|
||||
@attr.add_to(proxy)
|
||||
proxy.should have_received(:add_callback).with(@name, @block)
|
||||
end
|
||||
|
||||
it "should convert names to symbols" do
|
||||
|
|
|
@ -12,30 +12,26 @@ describe FactoryGirl::Attribute::Dynamic do
|
|||
end
|
||||
|
||||
it "should call the block to set a value" do
|
||||
@proxy = "proxy"
|
||||
stub(@proxy).set
|
||||
@proxy = stub("proxy", :set => nil)
|
||||
@attr.add_to(@proxy)
|
||||
@proxy.should have_received.set(@name, 'value')
|
||||
@proxy.should have_received(:set).with(@name, 'value')
|
||||
end
|
||||
|
||||
it "should yield the proxy to the block when adding its value to a proxy" do
|
||||
@block = lambda {|a| a }
|
||||
@attr = FactoryGirl::Attribute::Dynamic.new(:user, @block)
|
||||
@proxy = "proxy"
|
||||
stub(@proxy).set
|
||||
@proxy = stub("proxy", :set => nil)
|
||||
@attr.add_to(@proxy)
|
||||
@proxy.should have_received.set(:user, @proxy)
|
||||
@proxy.should have_received(:set).with(:user, @proxy)
|
||||
end
|
||||
|
||||
it "evaluates the block with in the context of the proxy without an argument" do
|
||||
result = 'other attribute value'
|
||||
@block = lambda { other_attribute }
|
||||
@attr = FactoryGirl::Attribute::Dynamic.new(:user, @block)
|
||||
@proxy = "proxy"
|
||||
stub(@proxy).set
|
||||
stub(@proxy).other_attribute { result }
|
||||
@proxy = stub("proxy", :set => nil, :other_attribute => result)
|
||||
@attr.add_to(@proxy)
|
||||
@proxy.should have_received.set(:user, result)
|
||||
@proxy.should have_received(:set).with(:user, result)
|
||||
end
|
||||
|
||||
it "should raise an error when defining an attribute writer" do
|
||||
|
@ -45,10 +41,10 @@ describe FactoryGirl::Attribute::Dynamic do
|
|||
end
|
||||
|
||||
it "should raise an error when returning a sequence" do
|
||||
stub(Factory).sequence { FactoryGirl::Sequence.new(:email) }
|
||||
Factory.stubs(:sequence => FactoryGirl::Sequence.new(:email))
|
||||
block = lambda { Factory.sequence(:email) }
|
||||
attr = FactoryGirl::Attribute::Dynamic.new(:email, block)
|
||||
proxy = stub!.set.subject
|
||||
proxy = stub("proxy")
|
||||
lambda {
|
||||
attr.add_to(proxy)
|
||||
}.should raise_error(FactoryGirl::SequenceAbuseError)
|
||||
|
|
|
@ -12,14 +12,15 @@ describe FactoryGirl::Attribute::Implicit do
|
|||
|
||||
context "with a known factory" do
|
||||
before do
|
||||
stub(FactoryGirl.factories).registered? { true }
|
||||
FactoryGirl.factories.stubs(:registered? => true)
|
||||
# stub(FactoryGirl.factories).registered? { true }
|
||||
end
|
||||
|
||||
it "associates the factory" do
|
||||
proxy = "proxy"
|
||||
stub(proxy).associate
|
||||
proxy = stub("proxy", :associate => nil)
|
||||
# stub(proxy).associate
|
||||
@attr.add_to(proxy)
|
||||
proxy.should have_received.associate(@name, @name, {})
|
||||
proxy.should have_received(:associate).with(@name, @name, {})
|
||||
end
|
||||
|
||||
it "is an association" do
|
||||
|
@ -37,10 +38,9 @@ describe FactoryGirl::Attribute::Implicit do
|
|||
end
|
||||
|
||||
it "generates the sequence" do
|
||||
proxy = "proxy"
|
||||
stub(proxy).set
|
||||
proxy = stub("proxy", :set => nil)
|
||||
@attr.add_to(proxy)
|
||||
proxy.should have_received.set(@name, "magic")
|
||||
proxy.should have_received(:set).with(@name, "magic")
|
||||
end
|
||||
|
||||
it "isn't an association" do
|
||||
|
|
|
@ -13,9 +13,8 @@ describe FactoryGirl::Attribute::Sequence do
|
|||
end
|
||||
|
||||
it "assigns the next value in the sequence" do
|
||||
proxy = "proxy"
|
||||
stub(proxy).set
|
||||
proxy = stub("proxy", :set => nil)
|
||||
@attr.add_to(proxy)
|
||||
proxy.should have_received.set(@name, "Name 5")
|
||||
proxy.should have_received(:set).with(@name, "Name 5")
|
||||
end
|
||||
end
|
||||
|
|
|
@ -12,9 +12,9 @@ describe FactoryGirl::Attribute::Static do
|
|||
end
|
||||
|
||||
it "should set its static value on a proxy" do
|
||||
@proxy = "proxy"
|
||||
mock(@proxy).set(@name, @value)
|
||||
@proxy = stub("proxy", :set => nil)
|
||||
@attr.add_to(@proxy)
|
||||
@proxy.should have_received(:set).with(@name, @value)
|
||||
end
|
||||
|
||||
it "should raise an error when defining an attribute writer" do
|
||||
|
|
|
@ -15,10 +15,9 @@ describe FactoryGirl::Attribute do
|
|||
end
|
||||
|
||||
it "should do nothing when being added to a proxy" do
|
||||
@proxy = "proxy"
|
||||
stub(@proxy).set
|
||||
@proxy = stub("proxy", :set => nil)
|
||||
@attr.add_to(@proxy)
|
||||
@proxy.should have_received.set.never
|
||||
@proxy.should have_received(:set).never
|
||||
end
|
||||
|
||||
it "should raise an error when defining an attribute writer" do
|
||||
|
|
|
@ -15,20 +15,22 @@ describe FactoryGirl::DefinitionProxy do
|
|||
end
|
||||
|
||||
it "should add a static attribute when an attribute is defined with a value" do
|
||||
attribute = 'attribute'
|
||||
stub(attribute).name { :name }
|
||||
mock(FactoryGirl::Attribute::Static).new(:name, 'value') { attribute }
|
||||
mock(factory).define_attribute(attribute)
|
||||
attribute = stub('attribute', :name => :name)
|
||||
FactoryGirl::Attribute::Static.stubs(:new => attribute)
|
||||
factory.stubs(:define_attribute)
|
||||
subject.add_attribute(:name, 'value')
|
||||
factory.should have_received(:define_attribute).with(attribute)
|
||||
FactoryGirl::Attribute::Static.should have_received(:new).with(:name, "value")
|
||||
end
|
||||
|
||||
it "should add a dynamic attribute when an attribute is defined with a block" do
|
||||
attribute = 'attribute'
|
||||
stub(attribute).name { :name }
|
||||
attribute = stub('attribute', :name => :name)
|
||||
block = lambda {}
|
||||
mock(FactoryGirl::Attribute::Dynamic).new(:name, block) { attribute }
|
||||
mock(factory).define_attribute(attribute)
|
||||
FactoryGirl::Attribute::Dynamic.stubs(:new => attribute)
|
||||
factory.stubs(:define_attribute)
|
||||
subject.add_attribute(:name, &block)
|
||||
FactoryGirl::Attribute::Dynamic.should have_received(:new).with(:name, block)
|
||||
factory.should have_received(:define_attribute).with(attribute)
|
||||
end
|
||||
|
||||
it "should raise for an attribute with a value and a block" do
|
||||
|
@ -54,85 +56,90 @@ describe FactoryGirl::DefinitionProxy do
|
|||
|
||||
describe "adding an attribute using a in-line sequence" do
|
||||
it "should create the sequence" do
|
||||
mock(FactoryGirl::Sequence).new(:name, 1)
|
||||
FactoryGirl::Sequence.stubs(:new)
|
||||
subject.sequence(:name) {}
|
||||
FactoryGirl::Sequence.should have_received(:new).with(:name, 1)
|
||||
end
|
||||
|
||||
it "should create the sequence with a custom default value" do
|
||||
mock(FactoryGirl::Sequence).new(:name, "A")
|
||||
FactoryGirl::Sequence.stubs(:new)
|
||||
subject.sequence(:name, "A") {}
|
||||
FactoryGirl::Sequence.should have_received(:new).with(:name, "A")
|
||||
end
|
||||
|
||||
it "should add a dynamic attribute" do
|
||||
attribute = 'attribute'
|
||||
stub(attribute).name { :name }
|
||||
mock(FactoryGirl::Attribute::Dynamic).new(:name, is_a(Proc)) { attribute }
|
||||
attribute = stub('attribute', :name => :name)
|
||||
FactoryGirl::Attribute::Dynamic.stubs(:new => attribute)
|
||||
subject.sequence(:name) {}
|
||||
factory.attributes.should include(attribute)
|
||||
FactoryGirl::Attribute::Dynamic.should have_received(:new).with(:name, is_a(Proc))
|
||||
end
|
||||
end
|
||||
|
||||
it "should add a callback attribute when the after_build attribute is defined" do
|
||||
mock(FactoryGirl::Attribute::Callback).new(:after_build, is_a(Proc)) { 'after_build callback' }
|
||||
FactoryGirl::Attribute::Callback.stubs(:new => "after_build callback")
|
||||
subject.after_build {}
|
||||
factory.attributes.should include('after_build callback')
|
||||
FactoryGirl::Attribute::Callback.should have_received(:new).with(:after_build, is_a(Proc))
|
||||
end
|
||||
|
||||
it "should add a callback attribute when the after_create attribute is defined" do
|
||||
mock(FactoryGirl::Attribute::Callback).new(:after_create, is_a(Proc)) { 'after_create callback' }
|
||||
FactoryGirl::Attribute::Callback.stubs(:new => "after_create callback")
|
||||
subject.after_create {}
|
||||
factory.attributes.should include('after_create callback')
|
||||
FactoryGirl::Attribute::Callback.should have_received(:new).with(:after_create, is_a(Proc))
|
||||
end
|
||||
|
||||
it "should add a callback attribute when the after_stub attribute is defined" do
|
||||
mock(FactoryGirl::Attribute::Callback).new(:after_stub, is_a(Proc)) { 'after_stub callback' }
|
||||
FactoryGirl::Attribute::Callback.stubs(:new => "after_stub callback")
|
||||
subject.after_stub {}
|
||||
factory.attributes.should include('after_stub callback')
|
||||
FactoryGirl::Attribute::Callback.should have_received(:new).with(:after_stub, is_a(Proc))
|
||||
end
|
||||
|
||||
it "should add an association without a factory name or overrides" do
|
||||
name = :user
|
||||
attr = 'attribute'
|
||||
stub(attr).name { name }
|
||||
mock(FactoryGirl::Attribute::Association).new(name, name, {}) { attr }
|
||||
attr = stub('attribute', :name => name)
|
||||
FactoryGirl::Attribute::Association.stubs(:new => attr)
|
||||
subject.association(name)
|
||||
factory.attributes.should include(attr)
|
||||
FactoryGirl::Attribute::Association.should have_received(:new).with(name, name, {})
|
||||
end
|
||||
|
||||
it "should add an association with overrides" do
|
||||
name = :user
|
||||
attr = 'attribute'
|
||||
attr = stub('attribute', :name => name)
|
||||
overrides = { :first_name => 'Ben' }
|
||||
stub(attr).name { name }
|
||||
mock(FactoryGirl::Attribute::Association).new(name, name, overrides) { attr }
|
||||
FactoryGirl::Attribute::Association.stubs(:new => attr)
|
||||
subject.association(name, overrides)
|
||||
factory.attributes.should include(attr)
|
||||
FactoryGirl::Attribute::Association.should have_received(:new).with(name, name, overrides)
|
||||
end
|
||||
|
||||
it "should add an attribute using the method name when passed an undefined method" do
|
||||
attribute = 'attribute'
|
||||
stub(attribute).name { :name }
|
||||
mock(FactoryGirl::Attribute::Static).new(:name, 'value') { attribute }
|
||||
attribute = stub('attribute', :name => :name)
|
||||
FactoryGirl::Attribute::Static.stubs(:new => attribute)
|
||||
subject.send(:name, 'value')
|
||||
factory.attributes.should include(attribute)
|
||||
FactoryGirl::Attribute::Static.should have_received(:new).with(:name, 'value')
|
||||
end
|
||||
|
||||
it "adds an attribute using when passed an undefined method and block" do
|
||||
attribute = 'attribute'
|
||||
stub(attribute).name { :name }
|
||||
attribute = stub('attribute', :name => :name)
|
||||
block = lambda {}
|
||||
mock(FactoryGirl::Attribute::Dynamic).new(:name, block) { attribute }
|
||||
FactoryGirl::Attribute::Dynamic.stubs(:new => attribute)
|
||||
subject.send(:name, &block)
|
||||
factory.attributes.should include(attribute)
|
||||
FactoryGirl::Attribute::Dynamic.should have_received(:new).with(:name, block)
|
||||
end
|
||||
|
||||
it "adds an implicit attribute when passed an undefined method without arguments or a block" do
|
||||
name = :user
|
||||
attr = 'attribute'
|
||||
stub(attr).name { name }
|
||||
mock(FactoryGirl::Attribute::Implicit).new(name, factory) { attr }
|
||||
attr = stub('attribute', :name => name)
|
||||
FactoryGirl::Attribute::Implicit.stubs(:new => attr)
|
||||
subject.send(name)
|
||||
factory.attributes.should include(attr)
|
||||
FactoryGirl::Attribute::Implicit.should have_received(:new).with(name, factory)
|
||||
end
|
||||
|
||||
it "adds an association when passed an undefined method with a hash including :factory key" do
|
||||
|
@ -140,17 +147,17 @@ describe FactoryGirl::DefinitionProxy do
|
|||
factory_name = :user
|
||||
overrides = { :first_name => 'Ben' }
|
||||
args = { :factory => factory_name }.merge(overrides)
|
||||
attr = 'attribute'
|
||||
stub(attr).name { name }
|
||||
mock(FactoryGirl::Attribute::Association).new(name, factory_name, overrides) { attr }
|
||||
attr = stub('attribute', :name => name)
|
||||
FactoryGirl::Attribute::Association.stubs(:new => attr)
|
||||
subject.send(name, args)
|
||||
factory.attributes.should include(attr)
|
||||
FactoryGirl::Attribute::Association.should have_received(:new).with(name, factory_name, overrides)
|
||||
end
|
||||
|
||||
it "delegates to_create" do
|
||||
result = 'expected'
|
||||
mock(factory).to_create { result }
|
||||
|
||||
factory.stubs(:to_create => result)
|
||||
subject.to_create.should == result
|
||||
factory.should have_received(:to_create)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -6,18 +6,18 @@ describe "accessing an undefined method on Factory that is defined on FactoryGir
|
|||
let(:args) { [1, 2, 3] }
|
||||
|
||||
before do
|
||||
stub($stderr).puts
|
||||
stub(FactoryGirl, method_name).returns { return_value }
|
||||
$stderr.stubs(:puts)
|
||||
FactoryGirl.stubs(method_name => return_value)
|
||||
|
||||
@result = Factory.send(method_name, *args)
|
||||
end
|
||||
|
||||
it "prints a deprecation warning" do
|
||||
$stderr.should have_received.puts(anything)
|
||||
$stderr.should have_received(:puts).with(anything)
|
||||
end
|
||||
|
||||
it "invokes that method on FactoryGirl" do
|
||||
FactoryGirl.should have_received.method_missing(method_name, *args)
|
||||
FactoryGirl.should have_received(method_name).with(*args)
|
||||
end
|
||||
|
||||
it "returns the value from the method on FactoryGirl" do
|
||||
|
@ -26,19 +26,8 @@ describe "accessing an undefined method on Factory that is defined on FactoryGir
|
|||
end
|
||||
|
||||
describe "accessing an undefined method on Factory that is not defined on FactoryGirl" do
|
||||
let(:method_name) { :magic_beans }
|
||||
|
||||
before do
|
||||
stub($stderr).puts { raise "Don't print a deprecation warning" }
|
||||
|
||||
begin
|
||||
Factory.send(method_name)
|
||||
rescue Exception => @raised
|
||||
end
|
||||
end
|
||||
|
||||
it "raises a NoMethodError" do
|
||||
@raised.should be_a(NoMethodError)
|
||||
expect { Factory.send(:magic_beans) }.to raise_error(NoMethodError)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -54,13 +43,7 @@ end
|
|||
|
||||
describe "accessing an undefined constant on Factory that is undefined on FactoryGirl" do
|
||||
it "raises a NameError for Factory" do
|
||||
begin
|
||||
Factory::BOGUS
|
||||
rescue Exception => exception
|
||||
end
|
||||
|
||||
exception.should be_a(NameError)
|
||||
exception.message.should include("Factory::BOGUS")
|
||||
expect { Factory::BOGUS }.to raise_error(NameError, /Factory::BOGUS/)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -27,46 +27,44 @@ describe FactoryGirl::Factory do
|
|||
|
||||
describe "after adding an attribute" do
|
||||
before do
|
||||
@attribute = "attribute"
|
||||
@proxy = "proxy"
|
||||
@attribute = stub("attribute", :name => :name, :add_to => nil)
|
||||
@proxy = stub("proxy", :result => "result", :set => nil)
|
||||
|
||||
stub(@attribute).name { :name }
|
||||
stub(@attribute).add_to
|
||||
stub(@proxy).set
|
||||
stub(@proxy).result { 'result' }
|
||||
stub(FactoryGirl::Attribute::Static).new { @attribute }
|
||||
stub(FactoryGirl::Proxy::Build).new { @proxy }
|
||||
FactoryGirl::Attribute::Static.stubs(:new => @attribute)
|
||||
FactoryGirl::Proxy::Build.stubs(:new => @proxy)
|
||||
|
||||
@factory.define_attribute(@attribute)
|
||||
end
|
||||
|
||||
it "should create the right proxy using the build class when running" do
|
||||
mock(FactoryGirl::Proxy::Build).new(@factory.build_class) { @proxy }
|
||||
FactoryGirl::Proxy::Build.stubs(:new => @proxy)
|
||||
@factory.run(FactoryGirl::Proxy::Build, {})
|
||||
FactoryGirl::Proxy::Build.should have_received(:new).with(@factory.build_class)
|
||||
end
|
||||
|
||||
it "should add the attribute to the proxy when running" do
|
||||
mock(@attribute).add_to(@proxy)
|
||||
@attribute.stubs(:add_to)
|
||||
@factory.run(FactoryGirl::Proxy::Build, {})
|
||||
@attribute.should have_received(:add_to).with(@proxy)
|
||||
end
|
||||
|
||||
it "should return the result from the proxy when running" do
|
||||
mock(@proxy).result(nil) { 'result' }
|
||||
@proxy.stubs(:result => "result")
|
||||
@factory.run(FactoryGirl::Proxy::Build, {}).should == 'result'
|
||||
@proxy.should have_received(:result).with(nil)
|
||||
end
|
||||
end
|
||||
|
||||
it "passes a custom creation block" do
|
||||
proxy = 'proxy'
|
||||
stub(FactoryGirl::Proxy::Build).new { proxy }
|
||||
stub(proxy).result {}
|
||||
proxy = stub("proxy", :result => nil)
|
||||
FactoryGirl::Proxy::Build.stubs(:new => proxy)
|
||||
block = lambda {}
|
||||
factory = FactoryGirl::Factory.new(:object)
|
||||
factory.to_create(&block)
|
||||
|
||||
factory.run(FactoryGirl::Proxy::Build, {})
|
||||
|
||||
proxy.should have_received.result(block)
|
||||
proxy.should have_received(:result).with(block)
|
||||
end
|
||||
|
||||
it "should return associations" do
|
||||
|
|
|
@ -2,7 +2,7 @@ require 'spec_helper'
|
|||
|
||||
share_examples_for "finds definitions" do
|
||||
before do
|
||||
stub(FactoryGirl).load
|
||||
FactoryGirl.stubs(:load)
|
||||
FactoryGirl.find_definitions
|
||||
end
|
||||
subject { FactoryGirl }
|
||||
|
@ -10,7 +10,7 @@ end
|
|||
|
||||
RSpec::Matchers.define :load_definitions_from do |file|
|
||||
match do |given|
|
||||
@has_received = have_received.method_missing(:load, File.expand_path(file))
|
||||
@has_received = have_received(:load).with(File.expand_path(file))
|
||||
@has_received.matches?(given)
|
||||
end
|
||||
|
||||
|
@ -79,10 +79,11 @@ describe "definition loading" do
|
|||
in_directory_with_files File.join(dir, 'factories', 'b.rb'),
|
||||
File.join(dir, 'factories', 'a.rb')
|
||||
it "should load the files in the right order" do
|
||||
@loaded = []
|
||||
stub(FactoryGirl).load { |a| @loaded << File.split(a)[-1] }
|
||||
FactoryGirl.stubs(:load)
|
||||
sorted_load_order = sequence("load order")
|
||||
FactoryGirl.expects(:load).with(includes("a.rb")).in_sequence(sorted_load_order)
|
||||
FactoryGirl.expects(:load).with(includes("b.rb")).in_sequence(sorted_load_order)
|
||||
FactoryGirl.find_definitions
|
||||
@loaded.should == ["a.rb", "b.rb"]
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ describe FactoryGirl::Proxy::AttributesFor do
|
|||
|
||||
describe "when asked to associate with another factory" do
|
||||
before do
|
||||
stub(FactoryGirl).create
|
||||
FactoryGirl.stubs(:create)
|
||||
@proxy.associate(:owner, :user, {})
|
||||
end
|
||||
|
||||
|
@ -21,9 +21,9 @@ describe FactoryGirl::Proxy::AttributesFor do
|
|||
end
|
||||
|
||||
it "should not call Factory.create when building an association" do
|
||||
stub(FactoryGirl).create
|
||||
FactoryGirl.stubs(:create)
|
||||
@proxy.association(:user).should be_nil
|
||||
FactoryGirl.should have_received.create.never
|
||||
FactoryGirl.should have_received(:create).never
|
||||
end
|
||||
|
||||
it "should always return nil when building an association" do
|
||||
|
|
|
@ -2,48 +2,40 @@ require 'spec_helper'
|
|||
|
||||
describe FactoryGirl::Proxy::Build do
|
||||
before do
|
||||
@class = Class.new
|
||||
@instance = "built-instance"
|
||||
|
||||
stub(@class).new { @instance }
|
||||
stub(@instance).attribute { 'value' }
|
||||
stub(@instance, :attribute=)
|
||||
stub(@instance, :owner=)
|
||||
|
||||
@proxy = FactoryGirl::Proxy::Build.new(@class)
|
||||
@instance = stub("built-instance", :attribute => "value", :attribute= => nil, :owner= => nil)
|
||||
@class = stub("class", :new => @instance)
|
||||
@proxy = FactoryGirl::Proxy::Build.new(@class)
|
||||
end
|
||||
|
||||
it "should instantiate the class" do
|
||||
@class.should have_received.new
|
||||
@class.should have_received(:new)
|
||||
end
|
||||
|
||||
describe "when asked to associate with another factory" do
|
||||
before do
|
||||
@association = "associated-instance"
|
||||
@associated_factory = "associated-factory"
|
||||
stub(FactoryGirl).factory_by_name { @associated_factory }
|
||||
stub(@associated_factory).run { @association }
|
||||
@associated_factory = stub("associated-factory", :run => @association)
|
||||
FactoryGirl.stubs(:factory_by_name => @associated_factory)
|
||||
@overrides = { 'attr' => 'value' }
|
||||
@proxy.associate(:owner, :user, @overrides)
|
||||
end
|
||||
|
||||
it "should create the associated instance" do
|
||||
@associated_factory.should have_received.run(FactoryGirl::Proxy::Create, @overrides)
|
||||
@associated_factory.should have_received(:run).with(FactoryGirl::Proxy::Create, @overrides)
|
||||
end
|
||||
|
||||
it "should set the associated instance" do
|
||||
@instance.should have_received.method_missing(:owner=, @association)
|
||||
@instance.should have_received(:owner=).with(@association)
|
||||
end
|
||||
end
|
||||
|
||||
it "should run create when building an association" do
|
||||
association = "associated-instance"
|
||||
associated_factory = "associated-factory"
|
||||
stub(FactoryGirl).factory_by_name { associated_factory }
|
||||
stub(associated_factory).run { association }
|
||||
associated_factory = stub("associated-factory", :run => association)
|
||||
FactoryGirl.stubs(:factory_by_name => associated_factory)
|
||||
overrides = { 'attr' => 'value' }
|
||||
@proxy.association(:user, overrides).should == association
|
||||
associated_factory.should have_received.run(FactoryGirl::Proxy::Create, overrides)
|
||||
associated_factory.should have_received(:run).with(FactoryGirl::Proxy::Create, overrides)
|
||||
end
|
||||
|
||||
it "should return the built instance when asked for the result" do
|
||||
|
@ -51,21 +43,20 @@ describe FactoryGirl::Proxy::Build do
|
|||
end
|
||||
|
||||
it "should run the :after_build callback when retrieving the result" do
|
||||
spy = Object.new
|
||||
stub(spy).foo
|
||||
@proxy.add_callback(:after_build, proc{ spy.foo })
|
||||
thing_to_call = stub("object", :foo => nil)
|
||||
@proxy.add_callback(:after_build, proc{ thing_to_call.foo })
|
||||
@proxy.result(nil)
|
||||
spy.should have_received.foo
|
||||
thing_to_call.should have_received(:foo)
|
||||
end
|
||||
|
||||
describe "when setting an attribute" do
|
||||
before do
|
||||
stub(@instance).attribute = 'value'
|
||||
@instance.stubs(:attributes=)
|
||||
@proxy.set(:attribute, 'value')
|
||||
end
|
||||
|
||||
it "should set that value" do
|
||||
@instance.should have_received.method_missing(:attribute=, 'value')
|
||||
@instance.should have_received(:attribute=).with('value')
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -75,7 +66,7 @@ describe FactoryGirl::Proxy::Build do
|
|||
end
|
||||
|
||||
it "should ask the built class for the value" do
|
||||
@instance.should have_received.attribute
|
||||
@instance.should have_received(:attribute)
|
||||
end
|
||||
|
||||
it "should return the value for that attribute" do
|
||||
|
|
|
@ -2,64 +2,53 @@ require 'spec_helper'
|
|||
|
||||
describe FactoryGirl::Proxy::Create do
|
||||
before do
|
||||
@class = Class.new
|
||||
@instance = "built-instance"
|
||||
|
||||
stub(@class).new { @instance }
|
||||
stub(@instance).attribute { 'value' }
|
||||
stub(@instance, :attribute=)
|
||||
stub(@instance, :owner=)
|
||||
stub(@instance).save!
|
||||
|
||||
@proxy = FactoryGirl::Proxy::Create.new(@class)
|
||||
@instance = stub("built-instance", :attribute => "value", :attribute= => nil, :owner= => nil, :save! => nil)
|
||||
@class = stub("class", :new => @instance)
|
||||
@proxy = FactoryGirl::Proxy::Create.new(@class)
|
||||
end
|
||||
|
||||
it "should instantiate the class" do
|
||||
@class.should have_received.new
|
||||
@class.should have_received(:new)
|
||||
end
|
||||
|
||||
describe "when asked to associate with another factory" do
|
||||
before do
|
||||
@association = "associated-instance"
|
||||
@associated_factory = "associated-factory"
|
||||
stub(FactoryGirl).factory_by_name { @associated_factory }
|
||||
stub(@associated_factory).run { @association }
|
||||
@associated_factory = stub("associated-factory", :run => @association)
|
||||
FactoryGirl.stubs(:factory_by_name => @associated_factory)
|
||||
@overrides = { 'attr' => 'value' }
|
||||
@proxy.associate(:owner, :user, @overrides)
|
||||
end
|
||||
|
||||
it "should create the associated instance" do
|
||||
@associated_factory.should have_received.run(FactoryGirl::Proxy::Create, @overrides)
|
||||
@associated_factory.should have_received(:run).with(FactoryGirl::Proxy::Create, @overrides)
|
||||
end
|
||||
|
||||
it "should set the associated instance" do
|
||||
@instance.should have_received.method_missing(:owner=, @association)
|
||||
@instance.should have_received(:owner=).with(@association)
|
||||
end
|
||||
end
|
||||
|
||||
it "should run create when building an association" do
|
||||
association = "associated-instance"
|
||||
associated_factory = "associated-factory"
|
||||
stub(FactoryGirl).factory_by_name { associated_factory }
|
||||
stub(associated_factory).run { association }
|
||||
associated_factory = stub("associated-factory", :run => association)
|
||||
FactoryGirl.stubs(:factory_by_name => associated_factory)
|
||||
overrides = { 'attr' => 'value' }
|
||||
@proxy.association(:user, overrides).should == association
|
||||
associated_factory.should have_received.run(FactoryGirl::Proxy::Create, overrides)
|
||||
associated_factory.should have_received(:run).with(FactoryGirl::Proxy::Create, overrides)
|
||||
end
|
||||
|
||||
describe "when asked for the result" do
|
||||
before do
|
||||
@build_spy = Object.new
|
||||
@create_spy = Object.new
|
||||
stub(@build_spy).foo
|
||||
stub(@create_spy).foo
|
||||
@build_spy = stub("build", :foo => nil)
|
||||
@create_spy = stub("create", :foo => nil)
|
||||
@proxy.add_callback(:after_build, proc{ @build_spy.foo })
|
||||
@proxy.add_callback(:after_create, proc{ @create_spy.foo })
|
||||
@result = @proxy.result(nil)
|
||||
end
|
||||
|
||||
it "should save the instance" do
|
||||
@instance.should have_received.save!
|
||||
@instance.should have_received(:save!)
|
||||
end
|
||||
|
||||
it "should return the built instance" do
|
||||
|
@ -67,17 +56,16 @@ describe FactoryGirl::Proxy::Create do
|
|||
end
|
||||
|
||||
it "should run both the build and the create callbacks" do
|
||||
@build_spy.should have_received.foo
|
||||
@create_spy.should have_received.foo
|
||||
@build_spy.should have_received(:foo)
|
||||
@create_spy.should have_received(:foo)
|
||||
end
|
||||
end
|
||||
|
||||
it "runs a custom create block" do
|
||||
block = 'custom create block'
|
||||
stub(block).call
|
||||
stub(@instance).save! { raise }
|
||||
block = stub('custom create block', :call => nil)
|
||||
@instance.stubs(:save!).raises(RuntimeError)
|
||||
instance = @proxy.result(block)
|
||||
block.should have_received.call(instance)
|
||||
block.should have_received(:call).with(instance)
|
||||
end
|
||||
|
||||
describe "when setting an attribute" do
|
||||
|
@ -86,7 +74,7 @@ describe FactoryGirl::Proxy::Create do
|
|||
end
|
||||
|
||||
it "should set that value" do
|
||||
@instance.should have_received.method_missing(:attribute=, 'value')
|
||||
@instance.should have_received(:attribute=).with('value')
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -96,7 +84,7 @@ describe FactoryGirl::Proxy::Create do
|
|||
end
|
||||
|
||||
it "should ask the built class for the value" do
|
||||
@instance.should have_received.attribute
|
||||
@instance.should have_received(:attribute)
|
||||
end
|
||||
|
||||
it "should return the value for that attribute" do
|
||||
|
|
|
@ -2,12 +2,8 @@ require 'spec_helper'
|
|||
|
||||
describe FactoryGirl::Proxy::Stub do
|
||||
before do
|
||||
@class = "class"
|
||||
@instance = "instance"
|
||||
stub(@class).new { @instance }
|
||||
stub(@instance, :id=)
|
||||
stub(@instance).id { 42 }
|
||||
stub(@instance).reload { @instance.connection.reload }
|
||||
@instance = stub("instance", :id= => nil, :id => 42)
|
||||
@class = stub("class", :new => @instance)
|
||||
|
||||
@stub = FactoryGirl::Proxy::Stub.new(@class)
|
||||
end
|
||||
|
@ -27,27 +23,35 @@ describe FactoryGirl::Proxy::Stub do
|
|||
describe "when a user factory exists" do
|
||||
before do
|
||||
@user = "user"
|
||||
stub(FactoryGirl).factory_by_name { @associated_factory }
|
||||
@associated_factory = 'associate-factory'
|
||||
@associated_factory = stub('associate-factory')
|
||||
FactoryGirl.stubs(:factory_by_name => @associated_factory)
|
||||
end
|
||||
|
||||
describe "when asked to associate with another factory" do
|
||||
before do
|
||||
stub(@instance).owner { @user }
|
||||
mock(@associated_factory).run(FactoryGirl::Proxy::Stub, {}) { @user }
|
||||
mock(@stub).set(:owner, @user)
|
||||
|
||||
@instance.stubs(:owner => @user)
|
||||
@associated_factory.stubs(:run => @user)
|
||||
@stub.stubs(:set)
|
||||
@stub.associate(:owner, :user, {})
|
||||
end
|
||||
|
||||
it "should set a value for the association" do
|
||||
@stub.result(nil).owner.should == @user
|
||||
end
|
||||
|
||||
it "should set the owner as the user" do
|
||||
@stub.should have_received(:set).with(:owner, @user)
|
||||
end
|
||||
|
||||
it "should create a stub correctly on the association" do
|
||||
@associated_factory.should have_received(:run).with(FactoryGirl::Proxy::Stub, {})
|
||||
end
|
||||
end
|
||||
|
||||
it "should return the association when building one" do
|
||||
mock(@associated_factory).run(FactoryGirl::Proxy::Stub, {}) { @user }
|
||||
@associated_factory.stubs(:run => @user)
|
||||
@stub.association(:user).should == @user
|
||||
@associated_factory.should have_received(:run).with(FactoryGirl::Proxy::Stub, {})
|
||||
end
|
||||
|
||||
describe "when asked for the result" do
|
||||
|
@ -56,11 +60,10 @@ describe FactoryGirl::Proxy::Stub do
|
|||
end
|
||||
|
||||
it "should run the :after_stub callback" do
|
||||
@spy = Object.new
|
||||
stub(@spy).foo
|
||||
@spy = stub("after_stub callback", :foo => nil)
|
||||
@stub.add_callback(:after_stub, proc{ @spy.foo })
|
||||
@stub.result(nil)
|
||||
@spy.should have_received.foo
|
||||
@spy.should have_received(:foo)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -68,8 +71,7 @@ describe FactoryGirl::Proxy::Stub do
|
|||
describe "with an existing attribute" do
|
||||
before do
|
||||
@value = "value"
|
||||
mock(@instance).send(:attribute) { @value }
|
||||
mock(@instance).send(:attribute=, @value)
|
||||
@instance.stubs(:attribute => @value, :attribute= => @value)
|
||||
@stub.set(:attribute, @value)
|
||||
end
|
||||
|
||||
|
@ -77,6 +79,15 @@ describe FactoryGirl::Proxy::Stub do
|
|||
@stub.attribute.should == 'value'
|
||||
end
|
||||
|
||||
it "should set the attribute as the value" do
|
||||
@instance.should have_received(:attribute=).with(@value)
|
||||
end
|
||||
|
||||
it "should retrieve the attribute" do
|
||||
@stub.attribute
|
||||
@instance.should have_received(:attribute)
|
||||
end
|
||||
|
||||
it "should return that value when asked for that attribute" do
|
||||
@stub.get(:attribute).should == @value
|
||||
end
|
||||
|
|
|
@ -14,7 +14,7 @@ describe FactoryGirl::Proxy do
|
|||
end
|
||||
|
||||
it "should call get for a missing method" do
|
||||
mock(@proxy).get(:name) { "it's a name" }
|
||||
@proxy.stubs(:get).with(:name).returns("it's a name")
|
||||
@proxy.name.should == "it's a name"
|
||||
end
|
||||
|
||||
|
@ -52,33 +52,31 @@ describe FactoryGirl::Proxy do
|
|||
|
||||
describe "when running callbacks" do
|
||||
before do
|
||||
@first_spy = Object.new
|
||||
@second_spy = Object.new
|
||||
stub(@first_spy).foo
|
||||
stub(@second_spy).foo
|
||||
@first_spy = stub("call_in_create", :foo => true)
|
||||
@second_spy = stub("call_in_create", :foo => true)
|
||||
end
|
||||
|
||||
it "should run all callbacks with a given name" do
|
||||
@proxy.add_callback(:after_create, proc{ @first_spy.foo })
|
||||
@proxy.add_callback(:after_create, proc{ @second_spy.foo })
|
||||
@proxy.run_callbacks(:after_create)
|
||||
@first_spy.should have_received.foo
|
||||
@second_spy.should have_received.foo
|
||||
@first_spy.should have_received(:foo).once
|
||||
@second_spy.should have_received(:foo).once
|
||||
end
|
||||
|
||||
it "should only run callbacks with a given name" do
|
||||
@proxy.add_callback(:after_create, proc{ @first_spy.foo })
|
||||
@proxy.add_callback(:after_build, proc{ @second_spy.foo })
|
||||
@proxy.run_callbacks(:after_create)
|
||||
@first_spy.should have_received.foo
|
||||
@second_spy.should_not have_received.foo
|
||||
@first_spy.should have_received(:foo).once
|
||||
@second_spy.should have_received(:foo).never
|
||||
end
|
||||
|
||||
it "should pass in the instance if the block takes an argument" do
|
||||
@proxy.instance_variable_set("@instance", @first_spy)
|
||||
@proxy.add_callback(:after_create, proc{|spy| spy.foo })
|
||||
@proxy.run_callbacks(:after_create)
|
||||
@first_spy.should have_received.foo
|
||||
@first_spy.should have_received(:foo).once
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,23 +4,14 @@ $LOAD_PATH << File.join(File.dirname(__FILE__))
|
|||
require 'rubygems'
|
||||
require 'rspec'
|
||||
require 'rspec/autorun'
|
||||
require 'rr'
|
||||
|
||||
require 'factory_girl'
|
||||
|
||||
module RR
|
||||
module Adapters
|
||||
module Rspec
|
||||
def self.included(mod)
|
||||
RSpec.configuration.backtrace_clean_patterns.push(RR::Errors::BACKTRACE_IDENTIFIER)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
require "mocha"
|
||||
require "bourne"
|
||||
|
||||
RSpec.configure do |config|
|
||||
config.mock_framework = :rr
|
||||
RSpec::Core::ExampleGroup.send(:include, RR::Adapters::Rspec)
|
||||
config.mock_framework = :mocha
|
||||
|
||||
config.after do
|
||||
FactoryGirl.factories.clear
|
||||
FactoryGirl.sequences.clear
|
||||
|
|
Loading…
Reference in a new issue