Another noob question about stubs

I have this function in my controller:
def open_id_authentication(openid_url)
authenticate_with_open_id(openid_url, :required => [:nickname,
:email], :optional => [:fullname]) do |result, identity_url,
registration|
if result.successful?
@user = User.find_or_initialize_by_identity_url(identity_url)
if @user.new_record?
@user.login = registration[‘nickname’]
@user.fullname = registration[‘fullname’]
@user.email = registration[‘email’]
@user.save!
@user.activate
end
self.current_user = @user
successful_login
else
failed_login result.message, ‘openid’
end
end
end

I tried to make a stub but with no success, maybe because it’s a
block. Any hint about that one please?

Thanks in advance

Pat

On 29 Apr 2008, at 17:00, Patrick A. wrote:

I tried to make a stub but with no success, maybe because it’s a
block. Any hint about that one please?

What are you trying to stub?

Are you trying to stub authenticate_with_open_id?

def open_id_authentication(openid_url)
authenticate_with_open_id(openid_url, :required => [:nickname,
:email], :optional => [:fullname]) do |result, identity_url,
registration|
if result.successful?
@user = User.find_or_initialize_by_identity_url(identity_url)

end
end
end

If so, you could try

controller.stub!
(:authenticate_with_open_id
).and_yield(:result, :identity_url, :registration)

so that the code in the block actually gets run (a normal stub, or one
with and_return with simply eat the block). Obviously, make sure that
it’s yielding the values you want it to.

HTH

Matt


Matt P. | Design & Code
| http://www.reprocessed.org/

ok thanks, I still get a “undefined method `successful?’”, successful?
is a method that is used inside of the block, it’s partof the
open_id_authentication plugin, any way to include the plugin in the
spec so that it recognizes the method?

thanks in advance

Pat

If you used Matt’s code below, then successful? is most likely
referring to a method after your open_id_authentication method.

When you stub! that method you’re effectively telling rspec to skip
over it and return what I tell you instead.

You’d need to paste more of your spec and controller action that
you’re stubbing for anyone to be of further help.

You’d need to paste more of your spec and controller action that you’re
stubbing for anyone to be of further help.

I think I posted it on my first post, here it is:

def open_id_authentication(openid_url)
authenticate_with_open_id(openid_url, :required => [:nickname,
:email], :optional => [:fullname]) do |result, identity_url,
registration|
if result.successful?
@user = User.find_or_initialize_by_identity_url(identity_url)
if @user.new_record?
@user.login = registration[‘nickname’]
@user.fullname = registration[‘fullname’]
@user.email = registration[‘email’]
@user.save!
@user.activate
end
self.current_user = @user
successful_login
else
failed_login result.message, ‘openid’
end
end
end

My spec looks like this:
it “should login with openid and redirect” do
openid_url = “patcito.myopenid.com
controller.stub!(:using_open_id?).and_return(true)
controller.stub!(:authenticate_with_open_id).and_yield(result,openid_url,registration={“nickname”=>“patcito”,“email”=>“[email protected]”,“fullname”=>‘bobby
bob’})
post :create, :openid_url => openid_url
session[:user_id].should_not be(nil)
response.should be_redirect
end

I tried to do something similar to this
http://pastie.textmate.org/102377 but it complains that
OpenID::Consumer::SUCCESS is not recognized. I guess I’d have to
include my open_id_authentication plugin lib in my spec to get it
recognized no? is there another way to include a lib in my spec to
make it work?

thanks in advance

Pat

On Wed, 2008-04-30 at 11:26 -0500, Patrick A. wrote:

My spec looks like this:
it “should login with openid and redirect” do
openid_url = “patcito.myopenid.com
controller.stub!(:using_open_id?).and_return(true)
controller.stub!(:authenticate_with_open_id).and_yield(result,openid_url,registration={“nickname”=>“patcito”,“email”=>“[email protected]”,“fullname”=>‘bobby
bob’})
post :create, :openid_url => openid_url
session[:user_id].should_not be(nil)
response.should be_redirect
end

result is not defined, so the successful? method isn’t known. I’d create
a mock for it and stub! that method.

result = mock(‘openid result’)
result.stub!(:successful?).and_return(true)

You can then use result.should_receive(:successful?) in subsequent specs
to describe what happens if the result of the call is and isn’t
successful.

Kind regards,

Hans

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs