2010-11-12 14:58:25 -06:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe "a generated stub instance" do
|
2011-01-19 19:47:49 -05:00
|
|
|
include FactoryGirl::Syntax::Methods
|
|
|
|
|
2010-11-12 14:58:25 -06:00
|
|
|
before do
|
|
|
|
define_model('User')
|
|
|
|
|
2012-03-09 17:20:38 -05:00
|
|
|
define_model('Post', title: :string,
|
|
|
|
body: :string,
|
|
|
|
age: :integer,
|
|
|
|
user_id: :integer) do
|
2010-11-12 14:58:25 -06:00
|
|
|
belongs_to :user
|
|
|
|
end
|
|
|
|
|
|
|
|
FactoryGirl.define do
|
2011-06-29 14:43:15 -04:00
|
|
|
factory :user
|
2010-11-12 14:58:25 -06:00
|
|
|
|
|
|
|
factory :post do
|
|
|
|
title { "default title" }
|
|
|
|
body { "default body" }
|
|
|
|
user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-09 17:20:38 -05:00
|
|
|
subject { build_stubbed(:post, title: 'overridden title') }
|
2010-11-12 14:58:25 -06:00
|
|
|
|
|
|
|
it "assigns a default attribute" do
|
|
|
|
subject.body.should == 'default body'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "assigns an overridden attribute" do
|
|
|
|
subject.title.should == 'overridden title'
|
|
|
|
end
|
|
|
|
|
|
|
|
it "assigns associations" do
|
|
|
|
subject.user.should_not be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "has an id" do
|
|
|
|
subject.id.should > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
it "generates unique ids" do
|
2011-01-19 19:47:49 -05:00
|
|
|
other_stub = build_stubbed(:post)
|
2010-11-12 14:58:25 -06:00
|
|
|
subject.id.should_not == other_stub.id
|
|
|
|
end
|
|
|
|
|
|
|
|
it "isn't a new record" do
|
|
|
|
should_not be_new_record
|
|
|
|
end
|
|
|
|
|
|
|
|
it "disables connection" do
|
|
|
|
lambda { subject.connection }.should raise_error(RuntimeError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "disables update_attribute" do
|
|
|
|
lambda { subject.update_attribute(:title, "value") }.should raise_error(RuntimeError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "disables reload" do
|
|
|
|
lambda { subject.reload }.should raise_error(RuntimeError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "disables destroy" do
|
|
|
|
lambda { subject.destroy }.should raise_error(RuntimeError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "disables save" do
|
|
|
|
lambda { subject.save }.should raise_error(RuntimeError)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "disables increment" do
|
|
|
|
lambda { subject.increment!(:age) }.should raise_error(RuntimeError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-10-12 09:35:10 -06:00
|
|
|
describe "calling `build_stubbed` with a block" do
|
|
|
|
include FactoryGirl::Syntax::Methods
|
|
|
|
|
|
|
|
before do
|
2012-03-09 17:20:38 -05:00
|
|
|
define_model('Company', name: :string)
|
2011-10-12 09:35:10 -06:00
|
|
|
|
|
|
|
FactoryGirl.define do
|
|
|
|
factory :company
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "passes the stub instance" do
|
2012-03-09 17:20:38 -05:00
|
|
|
build_stubbed(:company, name: 'thoughtbot') do |company|
|
2011-10-12 09:35:10 -06:00
|
|
|
company.name.should eq('thoughtbot')
|
|
|
|
expect { company.save }.to raise_error(RuntimeError)
|
|
|
|
end
|
|
|
|
end
|
2011-12-20 19:10:52 +01:00
|
|
|
|
|
|
|
it "returns the stub instance" do
|
|
|
|
expected = nil
|
|
|
|
build_stubbed(:company) do |company|
|
|
|
|
expected = company
|
|
|
|
"hello!"
|
|
|
|
end.should == expected
|
|
|
|
end
|
2011-10-12 09:35:10 -06:00
|
|
|
end
|