Hello, I''m trying to spec a Rails application using the couple restful_authentication/acl_system2 plugins. In my admin layout, I put the following code : <% restrict_to "admin" do -%> <ul id="admin-tabs"> <li> /users Users management </li> </ul> <% end -%> Then in my spec file ''spec/views/layout/admin.rhtml_spec.rb'' : describe "Layout Admin if admin user" do include UserMockViewHelper it "should display the admin tabs" do login_as :admin render ''layouts/admin'' response.should have_tag("ul#admin-tabs") end end the UserMockViewHelper module handle the mocking of the current user : module UserMockViewHelper def login_as(user) @current_user = mock_model(User) @role = mock_model(Role) case user when :admin @role.stub!(:map).and_return(["admin", "cms"]) when :cms_admin @role.stub!(:map).and_return(["cms", "cms_admin"]) when :cms @role.stub!(:map).and_return(["cms"]) else @role.stub!(:map).and_return([]) end @current_user.stub!(:roles).and_return(@role) @current_user.stub!(:login).and_return(user) User.should_receive(:find_by_id).any_number_of_times.and_return(@current_user) request.session[:user] = @current_user @controller.template.should_receive(:current_user).and_return(@current_user) end end When I run ''ruby script/spec spec/views/layouts/admin.rhtml_spec.rb'', I''ve got the following error message : ActionView::TemplateError in ''Layout Admin if admin user should display the admin tabs'' undefined local variable or method `current_user'' for #<Spec::Rails::DSL::ViewExampleController:0x3076d88> On line #28 of app/views/layouts/admin.rhtml 25: <ul> 26: <li> /articles Publications </li> 27: </ul> 28: <% restrict_to "admin" do -%> 29: <ul id="admin-tabs"> 30: <li> /users Utilisateurs </li> 31: </ul> #{RAILS_ROOT}/vendor/plugins/acl_system2/lib/caboose/access_control.rb:75:in `restrict_to'' (eval):2:in `send'' (eval):2:in `restrict_to'' #{RAILS_ROOT}/app/views/layouts/admin.rhtml:28:in `_run_rhtml_47app47views47layouts47admin46rhtml'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/base.rb:326:in `send'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/base.rb:326:in `compile_and_render_template'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/base.rb:301:in `render_template'' /usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_view/base.rb:260:in `render_file'' In the acl_system2 plugin, we have the following code : # restrict_to "admin | moderator" do # link_to "foo" # end def restrict_to(logicstring, context = {}) return false if current_user.nil? result = '''' if permit?(logicstring, context) result = yield if block_given? end result end I''m just starting with rspec and I believe I''m missing something ... Did someone run into this error before ? -- View this message in context: http://www.nabble.com/Rspec-and-acl_system2-plugin-tf4254003.html#a12106932 Sent from the rspec-users mailing list archive at Nabble.com.
Sorry, I''ve just seen that this question has already been raised in this mailing list. I should have double checked first. My bad :sleep: -- View this message in context: http://www.nabble.com/Rspec-and-acl_system2-plugin-tf4254003.html#a12107096 Sent from the rspec-users mailing list archive at Nabble.com.
On 8/11/07, Franck D''agostini <franck.dagostini at gmail.com> wrote:> > I''m trying to spec a Rails application using the couple > restful_authentication/acl_system2 plugins. > > In my admin layout, I put the following code : > > <% restrict_to "admin" do -%> > <ul id="admin-tabs"> > <li> /users Users management </li> > </ul> > <% end -%> >Remember, rSpec is Behavioural. The code behind "restrict_to" doesn''t matter; it''s the behaviour that matters. As such, your view-spec should only know about the code in that view. So for example, there''s no mention of a "user" or "role" model there, only a "restrict_to" method. Spec out the "restrict_to" code, rather than setting up a complex chain of fragile model interactions. This means that you can change the whole ACL system, but as long as it conforms to the same interface, restrict_to("rolename"), your specs will suceed. You can also change the associations or the name of your models, or add some fancy caching, anything, and this particular spec won''t die.> User.should_receive(:find_by_id).any_number_of_times.and_return(@current_user) > request.session[:user] = @current_userThe fact that you''re stubbing "find" calls in the *view* spec indicates that you''re probably testing things that are out of the scope of that spec. The actual ACL system should be tested on its own, not in the context of a view. Courtenay