spec/lib/simple_navigation_spec.rb in simple-navigation-2.2.2 vs spec/lib/simple_navigation_spec.rb in simple-navigation-2.2.3

- old
+ new

@@ -9,10 +9,32 @@ it "should be an empty hash after loading the module" do SimpleNavigation.config_files.should == {} end end + describe 'config_file?' do + before(:each) do + SimpleNavigation.stub!(:config_file_name => 'file_name') + end + it "should check for the file existance with the file_name" do + File.should_receive(:exists?).with('file_name') + SimpleNavigation.config_file?(:ctx) + end + context 'config file exists' do + before(:each) do + File.stub!(:exists? => true) + end + it {SimpleNavigation.config_file?(:ctx).should be_true} + end + context 'config file does not exist' do + before(:each) do + File.stub!(:exists? => false) + end + it {SimpleNavigation.config_file?(:ctx).should be_false} + end + end + describe 'config_file_name' do before(:each) do SimpleNavigation.config_file_path = 'path_to_config' end context 'for :default navigation_context' do @@ -29,43 +51,128 @@ SimpleNavigation.config_file_name(:WhyWouldYouDoThis).should == 'path_to_config/why_would_you_do_this_navigation.rb' end end end + describe 'set_template_from' do + before(:each) do + @context = stub :context + SimpleNavigation.stub!(:extract_controller_from => @controller) + end + it "should set the controller" do + @controller = stub(:controller) + SimpleNavigation.should_receive(:extract_controller_from).with(@context).and_return(@controller) + SimpleNavigation.should_receive(:controller=).with(@controller) + SimpleNavigation.set_template_from @context + end + it "should set the template" do + @template = stub(:template) + @controller = stub(:controller, :instance_variable_get => @template) + SimpleNavigation.stub!(:controller => @controller) + SimpleNavigation.should_receive(:template=).with(@template) + SimpleNavigation.set_template_from @context + end + end + + describe 'self.extract_controller_from' do + before(:each) do + @nav_context = stub(:nav_context) + end + + context 'object responds to controller' do + before(:each) do + @controller = stub(:controller) + @nav_context.stub!(:controller).and_return(@controller) + end + + it "should return the controller" do + SimpleNavigation.send(:extract_controller_from, @nav_context).should == @controller + end + + end + + context 'object does not respond to controller' do + it "should return the nav_context" do + SimpleNavigation.send(:extract_controller_from, @nav_context).should == @nav_context + end + end + end + + describe 'context_for_eval' do + context 'controller is present' do + before(:each) do + @controller = stub(:controller) + SimpleNavigation.stub!(:controller => @controller) + end + context 'template is present' do + before(:each) do + @template = stub(:template) + SimpleNavigation.stub!(:template => @template) + end + it {SimpleNavigation.context_for_eval.should == @template} + end + context 'template is not present' do + before(:each) do + SimpleNavigation.stub!(:template => nil) + end + it {SimpleNavigation.context_for_eval.should == @controller} + end + end + context 'controller is not present' do + before(:each) do + SimpleNavigation.stub!(:controller => nil) + end + context 'template is present' do + before(:each) do + @template = stub(:template) + SimpleNavigation.stub!(:template => @template) + end + it {SimpleNavigation.context_for_eval.should == @template} + end + context 'template is not present' do + before(:each) do + SimpleNavigation.stub!(:template => nil) + end + it {lambda {SimpleNavigation.context_for_eval}.should raise_error} + end + end + end + + describe 'load_config' do context 'config_file_path is set' do before(:each) do SimpleNavigation.config_file_path = 'path_to_config' #SimpleNavigation.stub!(:config_file_name => 'path_to_config/navigation.rb') end context 'config_file does exist' do before(:each) do - File.stub!(:exists?).and_return(true) + SimpleNavigation.stub!(:config_file? => true) IO.stub!(:read).and_return('file_content') end it "should not raise an error" do lambda{SimpleNavigation.load_config}.should_not raise_error end it "should read the specified config file from disc" do IO.should_receive(:read).with('path_to_config/navigation.rb') SimpleNavigation.load_config end it "should store the read content in the module (default context)" do - SimpleNavigation.should_receive(:config_file_name).with(:default).twice + SimpleNavigation.should_receive(:config_file_name).with(:default) SimpleNavigation.load_config SimpleNavigation.config_files[:default].should == 'file_content' end it "should store the content in the module (non default context)" do - SimpleNavigation.should_receive(:config_file_name).with(:my_context).twice + SimpleNavigation.should_receive(:config_file_name).with(:my_context) SimpleNavigation.load_config(:my_context) SimpleNavigation.config_files[:my_context].should == 'file_content' end end context 'config_file does not exist' do before(:each) do - File.stub!(:exists?).and_return(false) + SimpleNavigation.stub!(:config_file? => false) end it {lambda{SimpleNavigation.load_config}.should raise_error} end end \ No newline at end of file