require File.join(File.dirname(__FILE__), '../../spec_helper') require 'view_models/base' describe ViewModels::Base do describe "readers" do describe "model" do before(:each) do @model = stub :model @view_model = ViewModels::Base.new @model, nil end it "should have a reader" do @view_model.model.should == @model end end describe "controller" do before(:each) do @context = stub :controller @view_model = ViewModels::Base.new nil, @context end it "should have a reader" do @view_model.controller.should == @context end end end describe "context recognition" do describe "context is a view" do before(:each) do @view = stub :view, :controller => 'controller' @view_model = ViewModels::Base.new nil, @view end it "should get the controller from the view" do @view_model.controller.should == 'controller' end end describe "context is a controller" do before(:each) do @controller = stub :controller @view_model = ViewModels::Base.new nil, @controller end it "should just use it for the controller" do @view_model.controller.should == @controller end end end describe ".master_helper_module" do before(:each) do class ViewModels::SpecificMasterHelperModule < ViewModels::Base; end end it "should be a class specific inheritable accessor" do ViewModels::SpecificMasterHelperModule.master_helper_module = :some_value ViewModels::SpecificMasterHelperModule.master_helper_module.should == :some_value end it "should be an instance of Module on Base" do ViewModels::Base.master_helper_module.should be_instance_of(Module) end end describe ".controller_method" do it "should set up delegate calls to the controller" do ViewModels::Base.should_receive(:delegate).once.with(:method1, :method2, :to => :controller) ViewModels::Base.controller_method :method1, :method2 end end describe ".helper" do it "should include the helper" do helper_module = Module.new ViewModels::Base.should_receive(:include).once.with helper_module ViewModels::Base.helper helper_module end it "should include the helper in the master helper module" do master_helper_module = Module.new ViewModels::Base.should_receive(:master_helper_module).and_return master_helper_module helper_module = Module.new master_helper_module.should_receive(:include).once.with helper_module ViewModels::Base.helper helper_module end end describe "#logger" do it "should delegate to the controller" do controller = stub :controller view_model = ViewModels::Base.new nil, controller controller.should_receive(:logger).once in_the view_model do logger end end end end