1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00

Switches mocking library to rspec

This commit is contained in:
Andrew Mason 2017-06-01 09:54:02 -07:00 committed by Oliver Peate
parent 762370cbd9
commit 95188e2e78
22 changed files with 151 additions and 76 deletions

View file

@ -35,8 +35,6 @@ GEM
ffi (~> 1.9.10)
rspec-expectations (>= 2.99)
thor (~> 0.19)
bourne (1.6.0)
mocha (~> 1.1)
builder (3.2.2)
childprocess (0.5.9)
ffi (~> 1.0, >= 1.0.11)
@ -61,10 +59,7 @@ GEM
jdbc-sqlite3 (3.8.11.2)
json (1.8.6)
json (1.8.6-java)
metaclass (0.0.4)
minitest (5.9.1)
mocha (1.1.0)
metaclass (~> 0.0.1)
multi_json (1.11.2)
multi_test (0.1.2)
rake (10.5.0)
@ -107,11 +102,9 @@ DEPENDENCIES
activerecord-jdbcsqlite3-adapter
appraisal (~> 2.1.0)
aruba
bourne
cucumber (~> 1.3.15)
factory_girl!
jdbc-sqlite3
mocha (>= 0.12.8)
rspec (~> 3.0)
rspec-its (~> 1.0)
simplecov

View file

@ -28,8 +28,6 @@ Gem::Specification.new do |s|
s.add_development_dependency("timecop")
s.add_development_dependency("simplecov")
s.add_development_dependency("aruba")
s.add_development_dependency("mocha", ">= 0.12.8")
s.add_development_dependency("bourne")
s.add_development_dependency("appraisal", "~> 2.1.0")
s.add_development_dependency("activerecord", ">= 3.0.0")
s.add_development_dependency("yard")

View file

@ -25,9 +25,9 @@ describe "calling methods on the model instance" do
end
it "doesn't instantiate a record with attributes_for" do
User.stubs(:new)
allow(User).to receive(:new)
FactoryGirl.attributes_for(:user)
expect(User).to have_received(:new).never
expect(User).to_not have_received(:new)
end
end

View file

@ -4,10 +4,30 @@ describe FactoryGirl::Attribute::Association do
let(:name) { :author }
let(:factory) { :user }
let(:overrides) { { first_name: "John" } }
let(:association) { stub("association") }
let(:association) { double("association") }
subject { FactoryGirl::Attribute::Association.new(name, factory, overrides) }
before { subject.stubs(association: association) }
before do
RSpec.configure do |config|
config.mock_with :rspec do |mocks|
# FactoryGirl::Attribute::Association does not explicitly define an
# `association` instance method, so when we stub it out below,
# rspec-mock complains that it can't find the method.
#
# Therefore, temporarily turn off this feature.
mocks.verify_partial_doubles = false
end
end
allow(subject).to receive(:association).and_return association
end
after do
RSpec.configure do |config|
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
end
it { should be_association }
its(:name) { should eq name }

View file

@ -29,7 +29,23 @@ describe FactoryGirl::Attribute::Dynamic do
let(:result) { "other attribute value" }
before do
subject.stubs(attribute_defined_on_attribute: result)
RSpec.configure do |config|
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = false
end
end
allow(
subject,
).to receive(:attribute_defined_on_attribute).and_return result
end
after do
RSpec.configure do |config|
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
end
it "evaluates the attribute from the attribute" do

View file

@ -7,7 +7,7 @@ describe FactoryGirl::Declaration::Implicit do
context "with a known factory" do
before do
FactoryGirl.factories.stubs(:registered? => true)
allow(FactoryGirl.factories).to receive(:registered?).and_return true
end
it { should be_association }
@ -16,7 +16,7 @@ describe FactoryGirl::Declaration::Implicit do
context "with a known sequence" do
before do
FactoryGirl.sequences.stubs(:registered? => true)
allow(FactoryGirl.sequences).to receive(:registered?).and_return true
end
it { should_not be_association }

View file

@ -1,20 +1,27 @@
require "spec_helper"
describe FactoryGirl::DeclarationList, "#attributes" do
let(:static_attribute_1) { stub("static attribute 1") }
let(:static_attribute_2) { stub("static attribute 2") }
let(:dynamic_attribute_1) { stub("dynamic attribute 1") }
let(:static_declaration) { stub("static declaration", to_attributes: [static_attribute_1, static_attribute_2]) }
let(:dynamic_declaration) { stub("static declaration", to_attributes: [dynamic_attribute_1]) }
let(:static_attribute_1) { double("static attribute 1") }
let(:static_attribute_2) { double("static attribute 2") }
let(:dynamic_attribute_1) { double("dynamic attribute 1") }
let(:static_declaration) do
double(
"static declaration",
to_attributes: [static_attribute_1, static_attribute_2],
)
end
let(:dynamic_declaration) do
double("static declaration", to_attributes: [dynamic_attribute_1])
end
it "returns an AttributeList" do
expect(subject.attributes).to be_a(FactoryGirl::AttributeList)
end
let(:attribute_list) { stub("attribute list", define_attribute: true) }
let(:attribute_list) { double("attribute list", define_attribute: true) }
it "defines each attribute on the attribute list" do
FactoryGirl::AttributeList.stubs(new: attribute_list)
allow(FactoryGirl::AttributeList).to receive(:new).and_return attribute_list
subject.declare_attribute(static_declaration)
subject.declare_attribute(dynamic_declaration)
@ -28,9 +35,11 @@ describe FactoryGirl::DeclarationList, "#attributes" do
end
describe FactoryGirl::DeclarationList, "#declare_attribute" do
let(:declaration_1) { stub("declaration", name: "declaration 1") }
let(:declaration_2) { stub("declaration", name: "declaration 2") }
let(:declaration_with_same_name) { stub("declaration", name: "declaration 1") }
let(:declaration_1) { double("declaration", name: "declaration 1") }
let(:declaration_2) { double("declaration", name: "declaration 2") }
let(:declaration_with_same_name) do
double("declaration", name: "declaration 1")
end
context "when not overridable" do
it "adds the declaration to the list" do

View file

@ -87,7 +87,7 @@ describe FactoryGirl::DefinitionProxy, "#sequence" do
subject { FactoryGirl::Definition.new }
let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
before { FactoryGirl::Sequence.stubs(:new) }
before { allow(FactoryGirl::Sequence).to receive(:new) }
it "creates a new sequence starting at 1" do
proxy.sequence(:great)

View file

@ -9,15 +9,17 @@ describe FactoryGirl::Definition, "with a name" do
subject { FactoryGirl::Definition.new(name) }
it "creates a new attribute list with the name passed" do
FactoryGirl::DeclarationList.stubs(:new)
allow(FactoryGirl::DeclarationList).to receive(:new)
subject
expect(FactoryGirl::DeclarationList).to have_received(:new).with(name)
end
end
describe FactoryGirl::Definition, "#overridable" do
let(:list) { stub("declaration list", overridable: true) }
before { FactoryGirl::DeclarationList.stubs(new: list) }
let(:list) { double("declaration list", overridable: true) }
before do
allow(FactoryGirl::DeclarationList).to receive(:new).and_return list
end
it "sets the declaration list as overridable" do
expect(subject.overridable).to eq subject
@ -26,8 +28,8 @@ describe FactoryGirl::Definition, "#overridable" do
end
describe FactoryGirl::Definition, "defining traits" do
let(:trait_1) { stub("trait") }
let(:trait_2) { stub("trait") }
let(:trait_1) { double("trait") }
let(:trait_2) { double("trait") }
it "maintains a list of traits" do
subject.define_trait(trait_1)

View file

@ -1,18 +1,20 @@
require "spec_helper"
describe FactoryGirl::Decorator::DisallowsDuplicatesRegistry do
let(:registry) { stub("registry", name: 'Great thing', register: true) }
let(:registry) do
double("registry", name: "Great thing", register: true)
end
subject { described_class.new(registry) }
it "delegates #register to the registry when not registered" do
registry.stubs(registered?: false)
allow(registry).to receive(:registered?).and_return false
subject.register(:awesome, {})
expect(registry).to have_received(:register).with(:awesome, {})
end
it "raises when attempting to #register a previously registered strategy" do
registry.stubs(registered?: true)
allow(registry).to receive(:registered?).and_return true
expect { subject.register(:same_name, {}) }.
to raise_error(FactoryGirl::DuplicateDefinitionError, "Great thing already registered: same_name")
end

View file

@ -15,7 +15,11 @@ describe FactoryGirl::EvaluatorClassDefiner do
end
it "evaluates the block in the context of the evaluator" do
dependency_attribute = stub("dependency", name: :dependency, to_proc: -> { 1 })
dependency_attribute = double(
"dependency",
name: :dependency,
to_proc: -> { 1 },
)
dependency_attribute = stub_attribute(:dependency) { 1 }
attribute = stub_attribute(:attribute) { dependency + 1 }
evaluator = define_evaluator(attributes: [dependency_attribute, attribute])
@ -72,6 +76,6 @@ describe FactoryGirl::EvaluatorClassDefiner do
def stub_attribute(name = :attribute, &value)
value ||= -> {}
stub(name.to_s, name: name.to_sym, to_proc: value)
double(name.to_s, name: name.to_sym, to_proc: value)
end
end

View file

@ -17,8 +17,8 @@ describe FactoryGirl::Factory do
end
it "passes a custom creation block" do
strategy = stub("strategy", result: nil, add_observer: true)
FactoryGirl::Strategy::Build.stubs(new: strategy)
strategy = double("strategy", result: nil, add_observer: true)
allow(FactoryGirl::Strategy::Build).to receive(:new).and_return strategy
block = -> {}
factory = FactoryGirl::Factory.new(:object)
factory.to_create(&block)
@ -229,15 +229,20 @@ describe FactoryGirl::Factory, "running a factory" do
subject { FactoryGirl::Factory.new(:user) }
let(:attribute) { FactoryGirl::Attribute::Static.new(:name, "value", false) }
let(:declaration) { FactoryGirl::Declaration::Static.new(:name, "value", false) }
let(:strategy) { stub("strategy", result: "result", add_observer: true) }
let(:strategy) do
double("strategy", result: "result", add_observer: true)
end
let(:attributes) { [attribute] }
let(:attribute_list) { stub('attribute-list', declarations: [declaration], to_a: attributes) }
let(:attribute_list) do
double("attribute-list", declarations: [declaration], to_a: attributes)
end
before do
define_model("User", name: :string)
FactoryGirl::Declaration::Static.stubs(new: declaration)
declaration.stubs(to_attributes: attributes)
FactoryGirl::Strategy::Build.stubs(new: strategy)
allow(FactoryGirl::Declaration::Static).to receive(:new).
and_return declaration
allow(declaration).to receive(:to_attributes).and_return attributes
allow(FactoryGirl::Strategy::Build).to receive(:new).and_return strategy
subject.declare_attribute(declaration)
end

View file

@ -2,7 +2,7 @@ require 'spec_helper'
shared_examples_for "finds definitions" do
before do
FactoryGirl.stubs(:load)
allow(FactoryGirl).to receive(:load)
FactoryGirl.find_definitions
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 "loads the files in the right order" do
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)
wd = File.dirname(__FILE__)
file_b = File.join(wd, "tmp", dir, "factories", "b.rb")
file_a = File.join(wd, "tmp", dir, "factories", "a.rb")
allow(FactoryGirl).to receive(:load).with(file_a).ordered
allow(FactoryGirl).to receive(:load).with(file_b).ordered
FactoryGirl.find_definitions
end
end

View file

@ -1,8 +1,8 @@
require 'spec_helper'
describe FactoryGirl::Registry do
let(:registered_object) { stub("registered object") }
let(:second_registered_object) { stub("second registered object") }
let(:registered_object) { double("registered object") }
let(:second_registered_object) { double("second registered object") }
subject { FactoryGirl::Registry.new("Great thing") }

View file

@ -78,7 +78,7 @@ describe FactoryGirl::Sequence do
describe "a custom sequence and scope" do
subject { FactoryGirl::Sequence.new(:name, 'A') { |n| "=#{n}#{foo}" } }
let(:scope) { stub('scope', foo: 'attribute') }
let(:scope) { double("scope", foo: "attribute") }
it 'increments within the correct scope' do
expect(subject.next(scope)).to eq '=Aattribute'

View file

@ -2,7 +2,7 @@ require 'spec_helper'
describe FactoryGirl::Strategy::AttributesFor do
let(:result) { { name: "John Doe", gender: "Male", admin: false } }
let(:evaluation) { stub("evaluation", hash: result) }
let(:evaluation) { double("evaluation", hash: result) }
it_should_behave_like "strategy without association support"

View file

@ -1,6 +1,22 @@
require 'spec_helper'
describe FactoryGirl::Strategy::Create do
before do
RSpec.configure do |config|
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = false
end
end
end
after do
RSpec.configure do |config|
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
end
it_should_behave_like "strategy with association support", :create
it_should_behave_like "strategy with callbacks", :after_build, :before_create, :after_create
@ -18,7 +34,8 @@ describe FactoryGirl::Strategy::Create do
end
evaluation = evaluation_class.new
evaluation.stubs(object: nil, notify: nil)
allow(evaluation).to receive(:object)
allow(evaluation).to receive(:notify)
subject.result(evaluation)
expect(evaluation.block_run).to be true
end

View file

@ -32,7 +32,9 @@ describe FactoryGirl::Strategy::Stub do
end.new
end
let(:evaluation) { stub("evaluation", object: result_instance, notify: true) }
let(:evaluation) do
double("evaluation", object: result_instance, notify: true)
end
it { expect(subject.result(evaluation)).not_to be_new_record }
it { expect(subject.result(evaluation)).to be_persisted }

View file

@ -14,7 +14,10 @@ describe FactoryGirl::StrategyCalculator do
end
context "when a symbol" do
before { FactoryGirl.stubs(:strategy_by_name).returns(strategy) }
before do
allow(FactoryGirl).to receive(:strategy_by_name).and_return(strategy)
end
subject { FactoryGirl::StrategyCalculator.new(:build).strategy }
it "finds the strategy by name" do

View file

@ -8,14 +8,17 @@ require 'rspec/its'
require "simplecov"
require 'factory_girl'
require "mocha/api"
require "bourne"
require "timecop"
Dir["spec/support/**/*.rb"].each { |f| require File.expand_path(f) }
RSpec.configure do |config|
config.mock_framework = :mocha
config.mock_with :rspec do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on a
# real object. This is generally recommended, and will default to `true` in
# RSpec 4.
mocks.verify_partial_doubles = true
end
config.include DeclarationMatchers

View file

@ -12,14 +12,12 @@ RSpec::Matchers.define :delegate do |delegated_method|
end
match do |instance|
extend Mocha::API
@instance = instance
@args ||= []
return_value = 'stubbed return value'
method_on_target = @method_on_target || delegated_method
stubbed_target = stub('stubbed_target', method_on_target => return_value)
@instance.stubs(@target_method => stubbed_target)
stubbed_target = double("stubbed_target", method_on_target => return_value)
allow(@instance).to receive(@target_method).and_return stubbed_target
begin
@instance.send(delegated_method, *@args) == return_value
rescue NoMethodError

View file

@ -1,5 +1,5 @@
shared_examples_for "strategy without association support" do
let(:factory) { stub("associate_factory") }
let(:factory) { double("associate_factory") }
let(:attribute) { FactoryGirl::Attribute::Association.new(:user, :user, {}) }
def association_named(name, overrides)
@ -8,9 +8,9 @@ shared_examples_for "strategy without association support" do
end
before do
FactoryGirl.stubs(factory_by_name: factory)
factory.stubs(:compile)
factory.stubs(:run)
allow(FactoryGirl).to receive(:factory_by_name).and_return factory
allow(factory).to receive(:compile)
allow(factory).to receive(:run)
end
it "returns nil when accessing an association" do
@ -19,7 +19,7 @@ shared_examples_for "strategy without association support" do
end
shared_examples_for "strategy with association support" do |factory_girl_strategy_name|
let(:factory) { stub("associate_factory") }
let(:factory) { double("associate_factory") }
def association_named(name, strategy, overrides)
runner = FactoryGirl::FactoryRunner.new(name, strategy, [overrides])
@ -27,9 +27,9 @@ shared_examples_for "strategy with association support" do |factory_girl_strateg
end
before do
FactoryGirl.stubs(factory_by_name: factory)
factory.stubs(:compile)
factory.stubs(:run)
allow(FactoryGirl).to receive(:factory_by_name).and_return factory
allow(factory).to receive(:compile)
allow(factory).to receive(:run)
end
it "runs the factory with the correct overrides" do
@ -44,7 +44,7 @@ shared_examples_for "strategy with association support" do |factory_girl_strateg
end
shared_examples_for "strategy with strategy: :build" do |factory_girl_strategy_name|
let(:factory) { stub("associate_factory") }
let(:factory) { double("associate_factory") }
def association_named(name, overrides)
runner = FactoryGirl::FactoryRunner.new(name, overrides[:strategy], [overrides.except(:strategy)])
@ -52,9 +52,9 @@ shared_examples_for "strategy with strategy: :build" do |factory_girl_strategy_n
end
before do
FactoryGirl.stubs(factory_by_name: factory)
factory.stubs(:compile)
factory.stubs(:run)
allow(FactoryGirl).to receive(:factory_by_name).and_return factory
allow(factory).to receive(:compile)
allow(factory).to receive(:run)
end
it "runs the factory with the correct overrides" do
@ -75,7 +75,9 @@ shared_examples_for "strategy with callbacks" do |*callback_names|
end.new
end
let(:evaluation) { stub("evaluation", object: result_instance, notify: true, create: nil) }
let(:evaluation) do
double("evaluation", object: result_instance, notify: true, create: nil)
end
it "runs the callbacks #{callback_names} with the evaluation's object" do
subject.result(evaluation)