[rspec-users] Problems mocking controller helpers with the last version.

Juanma Cervera lists at ruby-forum.com
Mon Sep 29 05:33:33 EDT 2008


I have just installed the edge version of rspec and rspec-rails and some
of my specs related to views now fail.

I use the helper current_user in my views, to access the user that is
actually logged in.

I was simply using the sentence
assigns[:current_user] = stub_model(User,:role => "whatever")
and now, my spec fails in current_user.role because not it says that the
current_user is nil.

  You have a nil object when you didn't expect it!
  The error occurred while evaluating nil.role


I have try with something like this.

describe "/clientes/new.html.haml" do
  include ClientesHelper

  before do
    user = stub_model(User,:role => "Comercial")
    controller.stub!(:current_user).and_return(user)

    cliente = stub_model(Cliente, :new_record? => true)

    assigns[:cliente] = cliente
  end

  it "should show th New form" do
    render "/clientes/new.html.haml"
    response.should have_tag("form[action=?][method=post]",
clientes_path) do
      with_tag("input#cliente_codigo[name=?]", "cliente[codigo]")
      with_tag("input#cliente_nombre[name=?]", "cliente[nombre]")
      with_tag("input#cliente_domicilio[name=?]", "cliente[domicilio]")
      with_tag("input#cliente_poblacion[name=?]", "cliente[poblacion]")
      with_tag("input#cliente_provincia[name=?]", "cliente[provincia]")
      with_tag("input#cliente_telefono[name=?]", "cliente[telefono]")
    end
  end
end

and throw this error:
undefined local variable or method `controller' for
#<Spec::Rails::Example::ViewExampleGroup::Subclass_1:0xb70704ec>

Why?
How can I mock the current_user helper for using in my views?

Thanks
Juanma Cervera
-- 
Posted via http://www.ruby-forum.com/.


More information about the rspec-users mailing list