Skip to content

Commit

Permalink
added tests for get_current_wizard_step and next! and previous!
Browse files Browse the repository at this point in the history
  • Loading branch information
adkron committed May 30, 2008
1 parent 2b4f19e commit 7c6cd11
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
12 changes: 1 addition & 11 deletions lib/acts_as_wizard.rb
Expand Up @@ -36,7 +36,7 @@ def acts_as_wizard(*opts)

opts.each do |opt|
has_one opt, :dependent => :destroy
state opt, :after => :current_page
state opt
end

event :next do
Expand All @@ -52,11 +52,6 @@ def acts_as_wizard(*opts)
end

module InstanceMethods
# return the model for the current wizard page
def current_page
@current_page ||= find_page(self.get_current_wizard_step)
end

# returns a symbol for the current wizard page
def get_current_wizard_step
current_state
Expand Down Expand Up @@ -95,11 +90,6 @@ def get_wizard_page
def switch_wizard_page(direction)
send(direction)
end

private
def find_page(state)
page_class.send("find_by_#{self.class.to_s.underscore}_id",self.id)
end
end

module ClassMethods
Expand Down
39 changes: 31 additions & 8 deletions test/acts_as_wizard_test.rb
Expand Up @@ -23,13 +23,17 @@


class MainModel < ActiveRecord::Base
acts_as_wizard :first_page
acts_as_wizard :first_page, :second_page
end

class FirstPage < ActiveRecord::Base
acts_as_wizard_page :main_model
end

class SecondPage < ActiveRecord::Base
acts_as_wizard_page :main_model
end

class EmptyModel < ActiveRecord::Base
end

Expand All @@ -53,13 +57,6 @@ def test_no_errors
EmptyModel.acts_as_wizard(:foo)
end

def test_current_page_raises_error_if_model_does_not_exist
EmptyModel.acts_as_wizard(:foo)
@bunk_model = EmptyModel.new
e = assert_raise(NoMethodError) { @bunk_model.current_page }
assert_equal("undefined method `to_sym' for nil:NilClass", e.message)
end

def test_get_wizard_page_returns_a_new_first_page_when_there_is_no_first_page
@page = @main_model.get_wizard_page
assert_equal(FirstPage, @page.class)
Expand All @@ -73,4 +70,30 @@ def test_get_wizard_page_returns_the_first_page_when_there_is_a_first_page
assert_equal(@expected_page, @page)
end

def test_get_current_wizard_step_returns_first_page_on_new_model
assert_equal(:first_page, @main_model.get_current_wizard_step)
end

def test_get_current_wizard_step_returns_second_page_after_next_called
@main_model.next!
assert_equal(:second_page, @main_model.get_current_wizard_step)
end

def test_get_current_wizard_step_returns_first_page_after_next_called_and_then_previous_called
@main_model.next!
@main_model.previous!
assert_equal(:first_page, @main_model.get_current_wizard_step)
end

def test_get_current_wizard_step_returns_first_page_if_previous_called_on_first_page
@main_model.previous!
assert_equal(:first_page, @main_model.get_current_wizard_step)
end

def test_get_current_wizard_step_returns_second_page_after_next_called_and_next_called_again
@main_model.next!
@main_model.next!
assert_equal(:second_page, @main_model.get_current_wizard_step)
end

end
4 changes: 4 additions & 0 deletions test/schema.rb
Expand Up @@ -9,5 +9,9 @@

create_table :first_pages, :force => true do |t|
t.integer :main_model_id
end

create_table :second_pages, :force => true do |t|
t.integer :main_model_id
end
end

0 comments on commit 7c6cd11

Please sign in to comment.