mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
refactor tests (do not use so many stubs and mocks)
This commit is contained in:
parent
95b61a3e63
commit
ec82d2c4c3
3 changed files with 60 additions and 21 deletions
|
@ -1,5 +1,5 @@
|
||||||
ActiveRecord::Migration.suppress_messages do
|
ActiveRecord::Migration.suppress_messages do
|
||||||
%w{gates readers writers transients simples simple_new_dsls no_scopes no_direct_assignments thieves localizer_test_models persisted_states provided_and_persisted_states}.each do |table_name|
|
%w{gates readers writers transients simples simple_new_dsls no_scopes no_direct_assignments thieves localizer_test_models persisted_states provided_and_persisted_states with_enums with_true_enums with_false_enums}.each do |table_name|
|
||||||
ActiveRecord::Migration.create_table table_name, :force => true do |t|
|
ActiveRecord::Migration.create_table table_name, :force => true do |t|
|
||||||
t.string "aasm_state"
|
t.string "aasm_state"
|
||||||
end
|
end
|
||||||
|
|
|
@ -14,6 +14,54 @@ class Gate < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class WithEnum < ActiveRecord::Base
|
||||||
|
include AASM
|
||||||
|
|
||||||
|
# Fake this column for testing purposes
|
||||||
|
attr_accessor :aasm_state
|
||||||
|
|
||||||
|
aasm :enum => :test do
|
||||||
|
state :opened
|
||||||
|
state :closed
|
||||||
|
|
||||||
|
event :view do
|
||||||
|
transitions :to => :read, :from => [:needs_attention]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class WithTrueEnum < ActiveRecord::Base
|
||||||
|
include AASM
|
||||||
|
|
||||||
|
# Fake this column for testing purposes
|
||||||
|
attr_accessor :aasm_state
|
||||||
|
|
||||||
|
aasm :enum => true do
|
||||||
|
state :opened
|
||||||
|
state :closed
|
||||||
|
|
||||||
|
event :view do
|
||||||
|
transitions :to => :read, :from => [:needs_attention]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class WithFalseEnum < ActiveRecord::Base
|
||||||
|
include AASM
|
||||||
|
|
||||||
|
# Fake this column for testing purposes
|
||||||
|
attr_accessor :aasm_state
|
||||||
|
|
||||||
|
aasm :enum => false do
|
||||||
|
state :opened
|
||||||
|
state :closed
|
||||||
|
|
||||||
|
event :view do
|
||||||
|
transitions :to => :read, :from => [:needs_attention]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class Reader < ActiveRecord::Base
|
class Reader < ActiveRecord::Base
|
||||||
include AASM
|
include AASM
|
||||||
|
|
||||||
|
|
|
@ -64,46 +64,37 @@ describe "instance methods" do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "aasm_enum" do
|
describe "aasm_enum" do
|
||||||
subject { lambda{ gate.send(:aasm_enum) } }
|
|
||||||
|
|
||||||
context "when AASM enum setting contains an explicit enum method name" do
|
context "when AASM enum setting contains an explicit enum method name" do
|
||||||
let(:enum) { :test }
|
let(:with_enum) { WithEnum.new }
|
||||||
|
|
||||||
before :each do
|
|
||||||
AASM::StateMachine[Gate].config.stub(:enum).and_return(enum)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "returns whatever value was set in AASM config" do
|
it "returns whatever value was set in AASM config" do
|
||||||
expect(subject.call).to eq enum
|
expect(with_enum.send(:aasm_enum)).to eq :test
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "when AASM enum setting is simply set to true" do
|
context "when AASM enum setting is simply set to true" do
|
||||||
|
let(:with_true_enum) { WithTrueEnum.new }
|
||||||
before :each do
|
before :each do
|
||||||
AASM::StateMachine[Gate].config.stub(:enum).and_return(true)
|
WithTrueEnum.aasm.stub(:attribute_name).and_return(:value)
|
||||||
Gate.aasm.stub(:attribute_name).and_return(:value)
|
with_true_enum.stub(:aasm_guess_enum_method).and_return(:values)
|
||||||
gate.stub(:aasm_guess_enum_method).and_return(:values)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "infers enum method name from pluralized column name" do
|
it "infers enum method name from pluralized column name" do
|
||||||
expect(subject.call).to eq :values
|
expect(with_true_enum.send(:aasm_enum)).to eq :values
|
||||||
expect(gate).to have_received :aasm_guess_enum_method
|
expect(with_true_enum).to have_received :aasm_guess_enum_method
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "when AASM enum setting is explicitly disabled" do
|
context "when AASM enum setting is explicitly disabled" do
|
||||||
before :each do
|
let(:with_false_enum) { WithFalseEnum.new }
|
||||||
AASM::StateMachine[Gate].config.stub(:enum).and_return(false)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "returns nil" do
|
it "returns nil" do
|
||||||
expect(subject.call).to be_nil
|
expect(with_false_enum.send(:aasm_enum)).to be_nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "when AASM enum setting is not enabled" do
|
context "when AASM enum setting is not enabled" do
|
||||||
before :each do
|
before :each do
|
||||||
AASM::StateMachine[Gate].config.stub(:enum).and_return(nil)
|
|
||||||
Gate.aasm.stub(:attribute_name).and_return(:value)
|
Gate.aasm.stub(:attribute_name).and_return(:value)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -114,7 +105,7 @@ describe "instance methods" do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "infers enum method name from pluralized column name" do
|
it "infers enum method name from pluralized column name" do
|
||||||
expect(subject.call).to eq :values
|
expect(gate.send(:aasm_enum)).to eq :values
|
||||||
expect(gate).to have_received :aasm_guess_enum_method
|
expect(gate).to have_received :aasm_guess_enum_method
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -126,7 +117,7 @@ describe "instance methods" do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns nil, as we're not using enum" do
|
it "returns nil, as we're not using enum" do
|
||||||
expect(subject.call).to be_nil
|
expect(gate.send(:aasm_enum)).to be_nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue