Re: [rspec-users] Need immeadiate help

2009-09-30 Thread Jarkko Laine

On 30.9.2009, at 14.37, Mithun Perera wrote:

thanks Matt
Any other tutorial because i go through it. but no idea.
cheers,


http://lmgtfy.com/?q=rspec+introduction+tutorial

//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi

___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] Merb

2008-10-21 Thread Jarkko Laine

On 20.10.2008, at 22.05, Ashley Moran wrote:
There is an API for this though[2], and it was deprecated as of RC1,  
but has now apparently been reintroduced.


Lawrence Pit on the merb list explains the syntax:

#  Example
#   dispatch_to(MyController, :create, :name = 'Homer' ) do
|controller|
# controller.stub!(:current_user).and_return(@user)
#   end


Yeah, I was using that API to a great extent with a recent project and  
was pretty successful at doing outside-in development with rspec and  
merb. The syntax used in merb controller specs is a bit different from  
what Rails uses, but once you got around it, it was actually pretty  
cool and flexible. Here's a snippet from a controller spec in the  
project:


http://pastie.org/private/6uijgfwwzjngvhbkgxwqq

Note that I generally stub both render and display for the controller  
to make the tests run faster.


There's probably a lot of optimization left to do with the specs but I  
find the dispatch helpers pretty cool for building more specialized  
methods such as do_post.


I hope the merb folks will keep us outside-(s)inners in mind and not  
scrap the API.


Cheers,
//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://odesign.fi

Check out my latest book, Unobtrusive Prototype, fresh off the  
Peepcode oven:

http://peepcode.com/products/unobtrusive-prototype-js

___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] Any good conferences this year about ruby/rspec/watir?

2008-10-16 Thread Jarkko Laine

On 16.10.2008, at 16.17, juuuser wrote:

Hello.

Sorry for this little offtopic question, but I was wondering if anyone
happens to know if there's any good conferences coming up about  
rspec or

ruby or watir in this year where I could attend to?


Since you seem to be fluent in Finnish: http://rapidstraining.fi/ ;-)

//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi




smime.p7s
Description: S/MIME cryptographic signature
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Re: [rspec-users] How to define request.domain in when testing

2008-09-24 Thread Jarkko Laine

On 24.9.2008, at 12.51, Fernando Perez wrote:


Hi,

My application relies on request.domain for doing its job. The  
problem I

am encountering, is that when running tests, request.domain returns
test.host, how can I change that so that it returns mysite.com?


Set this somewhere in your test environment:

  ActionController::Base.default_url_options.update :host =  
'mysite.com'


//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] How to define request.domain in when testing

2008-09-24 Thread Jarkko Laine


On 24.9.2008, at 14.07, Fernando Perez wrote:


Hi jarkko,

I get the following error message:
---
`load_environment': undefined method `default_url_options' for
ActionController::Base:Class (NoMethodError)
---



From this

ticket:http://rails.lighthouseapp.com/projects/8994/tickets/22-default_url_options-is-being-ignored-by-named-route-optimisation

It was at that time expected to deprecate default_url_options, so  
maybe

it was removed from Rails 2.1?


Might be I've only played with it within action_mailer, where it still  
works:
config.action_mailer.default_url_options = { :host =  
development_url.com }


Not sure if it works the same way with ActionController, though. You  
can of course set the request host by hand in a setup method:


@request.host = host

//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] workflow: user = stories/features = spec = code = user

2008-09-19 Thread Jarkko Laine


On 19.9.2008, at 10.58, Matt Wynne wrote:


On 19 Sep 2008, at 02:00, DyingToLearn wrote:


Hi,

I am still trying to learn how to effectively use BDD. I would like  
to

know if there is a typical BDD workflow or if it is really just a
question of personal preference.

So far I see the following steps being fairly common:

1. talk with user to establish a few stories and features
2. write plaintext stories or cucumber features (with the user?)
3. write specs for whatever step needs to be wnitten
4. code until specs pass
5. write story/feature steps
6. repeat steps 3-5 until the story/feature passes
7. show the user to confirm that it works the way the user want (and
go back to step 2 if it doesn't)
8. go back to step 1 for new stories/features


This seems to be pretty much the workflow that's evolving for us,  
though I tend to write the step implementations before I start  
coding the views, controller, models. (i.e. 5 before 3)/


Ditto, and also, don't write all the specs at a time. My plan for 3-5  
is something like this:


3. Write the first step of the feature in question
4. Write the view spec to satisfy the step code
5. Write the view functionality to make 4 pass
6. Write the controller spec to satisfy the needs of 4
7. Write the code to make 6 pass
8. Write the model spec to satisfy the needs of 6
9. Write the code to make 8 pass
At this point the step should pass, so go back to 3 and repeat with  
the next step.


Note that I left out the refactoring part of the TDD cycle here. It  
generally happens between every layer after the specs pass.


//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] Best practices for sharing state between story steps?

2008-09-09 Thread Jarkko Laine

On 10.9.2008, at 0.29, Zach Dennis wrote:

I don't think this works as you may expect since My
SQL and PostgreSQL don't support nested transactions, which is what
would happen if your test environment wrapped stories/scenarios in
transactions and your application utilized transactions.


I've been bitten by this a couple of times. A common idiom in the  
Rails world is (was at least) to use save! to raise an exception when  
an object couldn't be saved. In a rescue block you could then do the  
things you wanted to do when saving failed. Raising the exception also  
rolled back a transaction, so it was a nice way to ensure some  
transactional logic:


MyModel.transaction do
  MyModel.save!
  OtherModel.save!
end

If either line in the transaction failed, it was ensured that neither  
of them was persisted in the db. This helped in many cases where you  
wanted either all or none of your updates to get into the db. However,  
with stories these cases always fail, because of the reason Zach  
mentions.


The question whether a case where someone posts an empty email address  
is an exceptional state in strict PragProg sense might be a bit  
questionable, of course, but it's beyond the point here...


//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] webrat question, form without buttons

2008-09-03 Thread Jarkko Laine


On 3.9.2008, at 19.38, Zach Dennis wrote:

On Wed, Sep 3, 2008 at 1:31 PM, Nick Hoffman [EMAIL PROTECTED]  
wrote:

On 2008-09-03, at 13:15, Zach Dennis wrote:


On Wed, Sep 3, 2008 at 12:40 PM, Nick Hoffman  
[EMAIL PROTECTED] wrote:


You can submit a form using Javascript.


That's why we don't have submit buttons. =)

But in the context of webrat, if there is a form w/o a submit  
button,
how can you submit it? Obviossly, clicks_button doesn't work  
anymore.

Thanks,


During regular use in a browser, how is your form submitted?



When the user clicks a button, but all of the handy dandy work is done
in unobtrusive-style javascript. It's not a submit button. It's a
CSS-made button with listeners attached,


If you're going unobtrusive, why not go all the way and have a real  
submit button there that gets replaced by the javascript with your  
custom button?


//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] Does RSpec work nicely with UUID primary keys?

2008-07-05 Thread Jarkko Laine

On 5.7.2008, at 18.37, John Knox wrote:


David Chelimsky wrote:

What happens when you run the generated examples?


My code uses single table inheritance (STI) and a MySQL database, so I
thought it best to create some new simple projects with a single  
model.


(1) I created a new project using SQLite with a single model.  All 58
auto-generated specifications ran successfully.

(2) I created a new single-model project using SQLite but added a UUID
primary key.  All 58 auto-generated specifications ran successfully.

(3) I created a new project using MySQL with a single model.  Again,  
all

58 auto-generated specifications ran successfully.

(4) (2) I created a new single-model project using MySQL but added a
UUID primary key.  All 58 auto-generated specifications ran
successfully.

Hmm.  So, it's not the UUIDs that are causing problems with my
application... but perhaps single table inheritance.  Will do more  
tests

and post again when I've better defined the problem.


The thing is, the scaffolding specs are specs for the behaviour of the  
scaffolded controllers/models/views. If you want to change that  
behaviour, your specs should reflect that assumption. If you add some  
new code (without writing new specs for it), even in the form of using  
plugins,  and your existing specs still run, it doesn't mean your code  
is working correctly because you didn't write specs for that part of  
the functionality yet. Using UUID probably changes things so little  
that (especially if/when they mock the models) it won't trip the  
scaffolding specs per se.


//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi



smime.p7s
Description: S/MIME cryptographic signature
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Re: [rspec-users] Mocking Models in Controller Specs...

2008-05-27 Thread Jarkko Laine


On 27.5.2008, at 10.52, Mark Dodwell wrote:

Also, any opinions on using a Factory pattern for instantiating  
mocks of

models? I like the idea of maintaining mocks in one place as I
frequently use them across several specs for models that have
associations. Do you think it would create dependency issues between  
the

specs using that Factory?


I have built my own factory by creating methods such as mock_user in  
the spec helper. That method will then fill in some reasonable values  
and stub all the needed methods. Granted, the need for that is less  
now with stub_model, but sometimes you also want to stub some  
associations. That has certainly reduced the amount of repetition in  
my specs.


//jarkko




~ Mark
--
Posted via http://www.ruby-forum.com/.
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi




smime.p7s
Description: S/MIME cryptographic signature
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Re: [rspec-users] mock_model not stubbing model attribtues in view spec

2008-05-20 Thread Jarkko Laine


On 20.5.2008, at 14.26, Ashley Moran wrote:



On 20 May 2008, at 11:50, Doug Livesey wrote:


Obviously, the view calls for a number of attributes from the
@input_timesheet, but unless I explicitly define these in the  
mock_model

method, I get the following kind of error:



I'm puzzled, I thought you always had to stub methods on a mock from  
mock_model?  I don't remember the default behaviour ever being to  
ignore unknown messages?


Sorry, guess this is not a helpful post, I just wanted to know how  
it behaves for you normally.


mock_model doesn't stub any methods automatically, and it doesn't  
matter whether you're in view, model or controller specs. If you need  
the attributes and don't want to specify anything by hand, stub_model  
might work for you. It basically creates a real AR object, with its db  
connections cut.


//jarkko




Ashley


--
http://www.patchspace.co.uk/
http://aviewfromafar.net/



___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi




smime.p7s
Description: S/MIME cryptographic signature
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Re: [rspec-users] Everyone Using Rspec Autotest?

2008-05-03 Thread Jarkko Laine


On 3.5.2008, at 22.18, Andrew Brown wrote:

I haven't been spec'ing lately, but been hopping back on and adding  
autotest.

However I get an error:

http://pastie.caboo.se/177538/wrap

I also noticed a post by Scott and there was mention of  
rspec_autotest.

Is this is what everyone is using?


I'm just using the one that comes with ZenTest. No need for anything  
else, just run 'autotest' in the root of your Rails app.


//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi




smime.p7s
Description: S/MIME cryptographic signature
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Re: [rspec-users] Rspecing this simple controller it is driving me mad...

2008-04-13 Thread Jarkko Laine


On 12.4.2008, at 14.06, roberto belardo wrote:


Uhm ok, i understood your answer but autotest did not.
Better, this do not solves the problem.
If i simply remove the stub and the should_receive
for the author field, autotest will complain like
this:

Spec::Mocks::MockExpectationError in
'CommentsController handling POST /comments should
create a new comment'
Mock 'Comment_1008' received unexpected message
:author= with (#User:0x..fdb98f624
@name=User_1006)

and this was the reason why i put the should_receive
and the stub on author= method, as someone told
previously on this ml.
:(

Is there someone who knows how to spec this? Help
needed.


Two ways:

1) either use Comment.new instead of mock_model(Comment) in your  
before block. This way your comment object has the ability to assign  
its author.
2) Use the mock model and your should_receive test. However, after  
that you cannot test that @comment.author.should be(@user) because you  
have stubbed author= and it doesn't really do anything. So you just  
have to rely on that it works (like you should since it's Rails code  
which you shouldn't really be testing).


//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi




smime.p7s
Description: S/MIME cryptographic signature
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Re: [rspec-users] how to stub the :current_user and any methods associated with it

2008-04-13 Thread Jarkko Laine


On 12.4.2008, at 22.24, newbie wrote:


Hey Jarkko,

Thanks for the feedback. I tried your suggestion and it doesn't seem
to work.


What doesn't work?




To quickly recap I'm just testing in my controller that this works

def index
@entries = current_user.entries
end

describe on the main page do
it should show all the current entries for the user do
   get :index
   assigns[:entries].should == @entries
end


I don't see you logging in a user anywhere.

//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi




smime.p7s
Description: S/MIME cryptographic signature
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Re: [rspec-users] how to stub the :current_user and any methods associated with it

2008-04-11 Thread Jarkko Laine


On 11.4.2008, at 4.57, newbie wrote:


Thanks for the direction. I'm glad to hear that I'm going in the right
direction. I just have a final question and I seem to be hitting a
stub/mocking road block at the moment. In my controller I have this...

def index
@entries = current_user.entries
end

How would I rspec this out correctly? This is what I have but it looks
wrong.

 before(:each) do
   @current_user = mock_model(User, :id = 1)
   @entry = mock_model(Entry, :user_id = 1)
   controller.stub!(:current_user).and_return(@current_user)
   controller.stub!(:entries).and_return(@entry)
 end


 describe on the index page do
   it should show all of the current_users entries do

@current_user.should_receive(:entries).with(@entry).and_return(true)


This is wrong. The with method means parameters the expected method  
call receives. In your case it gets no parameters. Besides, since you  
stub the entries method anyway, you can just specify the behaviour.


What you probably want with the code above is this:

assigns[:entries].should == @entry

//jarkko



   end
 end










On Apr 7, 11:31 am, Bryan Ray [EMAIL PROTECTED] wrote:

Responses shown below...

Sorry this isn't extremely informative ... in a bit of a rush.  
Hopefully it

will point you in the appropriate direction.



On Sun, Apr 6, 2008 at 2:17 PM, newbie [EMAIL PROTECTED] wrote:
I'm new to Rspec and I'm having a bit of trouble with this  
controller
that I'm testing. I think I'm doing it right but I guess my syntax  
is

wrong.
I'm currently using the acts_as_authenticated plug in.



What I want to test out is



EventController on entering the tickets_page
- should show the tickets_page  if the current_user has NOT entered
this page today



-
Below is my event controller
-



class EventController  ApplicationController



 before_filter :login_required



 def tickets_page
 if current_user.has_already_entered_today?
   flash[:notice] = 'Come back tomorrow'
   redirect_to :action = 'home'
 else
   flash[:notice] = 'Welcome'
 end
 end
end



-
Below is my rspec for this controller
-



require File.dirname(__FILE__) + '/../spec_helper'
describe EventController do



 before(:each) do
   @current_user = mock_model(User, :id = 1)
   controller.stub!(:current_user).and_return(@current_user)
   controller.stub!(:login_required).and_return(:true)
 end



 fixtures :users



 describe on entering the tickets page do
   it should show the tickets_page  if the current_user has NOT
entered this page today do
controller.current_user.stub!
(:has_already_entered_today?).and_return(:false)
get :tickets_page


controller 
.current_user.should_receive(:has_already_entered_today?).with(:f  
alse).and_return(:false)

response.should render_template(:tickets_page)
   end
end



end



-
My errors
-
Mock 'User_1001' received unexpected
message :has_already_entered_today? with (no args)


So this is a good start ... it means that the controller action is  
actually
being mocked out ... you just need to keep moving forward mocking/ 
stubbing
out the rest of your controller actions. it's complaining that it  
doesn't
know what to do on your *current_user.has_already_entered_today?*  
method.


@current_user.stub!(:has_already_entered_today?).and_return(insert  
true or

false here)




Anyone can help with any direction of what I might be doing wrong?



___
rspec-users mailing list
[EMAIL PROTECTED]
http://rubyforge.org/mailman/listinfo/rspec-users


--
Bryan Rayhttp://www.bryanray.net

Programming today is a race between software engineers striving to  
build
bigger and better idiot-proof programs, and the Universe trying to  
produce

bigger and better idiots. So far, the Universe is winning.

___
rspec-users mailing list
[EMAIL PROTECTED]://rubyforge.org/mailman/listinfo/rspec-users

___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi




smime.p7s
Description: S/MIME cryptographic signature
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Re: [rspec-users] Stories - Use view matchers rather than assigns[].should?

2008-04-09 Thread Jarkko Laine


On 10.4.2008, at 6.20, Tim Haines wrote:
Thanks Pat and David.  I'm implementing paging (via will_paginate)  
and thought I should start with a story.  I think I grok what you're  
saying Pat - the stories should only be looking at externally  
visible stuff - i.e. what a real user can see or do..   That's why  
you consider controller.send(current_user=,..) a bit hacky right?


Yes. Just create a given that goes through the normal logging process.  
You can parameterize it so that you can reuse it with different roles.


//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi




smime.p7s
Description: S/MIME cryptographic signature
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Re: [rspec-users] how to mock/stub restful_authenticated and acl2-ized act

2008-04-08 Thread Jarkko Laine


On 8.4.2008, at 23.17, Giuseppe Bertini wrote:


Hi Glenn, thanks for the response.


I would do something like this:

user = mock_model(User, :operator = true)
controller.stub!(:current_user).and_return(user)


OK, so, :operator is not a User attribute. Rather, a user is an
operator, so to speak, if a habtm relationship exists between the user
and the role titled 'operator'. If I modify your code like this:

   role = mock_model(Role, :title = 'operator')
   user = mock_model(User, :roles = [role])
   controller.stub!(:current_user).and_return(user)

things appear to be working well.
Thank you again for your help!


You might want to take that one step further and create a method such  
as has_role?(:operator) for the user class. That way you don't have to  
stub arrays  like roles in your specs, you just stub that method to  
return true or false and you're done. That way you make your code more  
easily speccable:


def has_role?(role)
  roles.map(:title).include?(role.to_s)
end

//jarkko




Giuseppe
--
Posted via http://www.ruby-forum.com/.
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi




smime.p7s
Description: S/MIME cryptographic signature
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Re: [rspec-users] Rspecing this simple controller it is driving me mad...

2008-04-08 Thread Jarkko Laine


On 8.4.2008, at 23.06, roberto belardo wrote:


Everything seemed so fine, but when i tried to
investigate a little i discovered this tremendous
behaviour of the spec:

--- CommentsController
def create
 @comment = Comment.new(params[:comment])
 @user = User.find(params[:user_id])
 @comment.author = 'foobar'
 @comment.save
end

--- CommentsController
Spec
describe CommentsController, handling POST/comments
do
 before(:each) do
   @user = mock_model(User, :to_param = 2)
   @comment = mock_model(Comment, :to_param = 1,
:author = @user)

User.should_receive(:find).at_least(1).times.and_return(@user)
   Comment.stub!(:new).and_return(@comment)
   @comment.stub!(:author=)
 end

 def post_with_successful_save
   @comment.should_receive(:save).and_return(true)
   @comment.should_receive(:author=)
   post :create, :user_id = @user_id, :comment = {}
 end

 it should create a new comment do

Comment.should_receive(:new).with({}).and_return(@comment)
   post_with_successful_save
   @comment.author.should be(@user)
 end
end

--- Autotest:
... 0 failures

How can the spec pass if i test that @comment.author
should be @user but in the controller i set a
fobar string in the @comment.author?


Because @comment.should_receive(:author=) stubs the method so it has  
no effect after that. I would advice against using it in a general  
purpose helper such as post_with_successful_save and instead use it in  
an actual spec if needed.





Better question: wich is the right way to test that
create action to be sure, it will set the author of
the comment?


Since you're testing that @comment.author.should be(@user), I see no  
reason to explicitly have @comment.should_receive(:author=). The  
former correctly tests the behaviour, whereas the latter checks the  
actual implementation, which in this case is superfluous. I hardly  
ever use should_receive with a setter method such as author=, but it  
might just be me.


Cheers,
//jarkko





--- Jarkko Laine [EMAIL PROTECTED] ha scritto:

...


Short answer: you haven't stubbed the author= method
for the comment
mock, only author.

A bit longer answer: Are you sure you can trust the
user to only
submit valid user ids? With your current code,
anyone can change the
user_id parameter in the call and fake any user id
she wants. This
might be intentional (if there are only admin users
in your app), just
pointing it out. You can also simplify your action
by just saying
@user.comments.create(params[:comment]) once you
have the user fetched.

Cheers,
//jarkko



Thanks in advance,
Roberto.


Inviato da Yahoo! Mail.
La casella di posta intelligente.
http://it.docs.yahoo.com/mail/overview/index.html
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi



___

rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users




 Inviato da Yahoo! Mail.
La casella di posta intelligente.
http://it.docs.yahoo.com/mail/overview/index.html
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi




smime.p7s
Description: S/MIME cryptographic signature
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Re: [rspec-users] Testing helper methods in rspec

2008-03-12 Thread Jarkko Laine
On Wed, Mar 12, 2008 at 3:54 PM, James B. Byrne [EMAIL PROTECTED] wrote:
 This is perhaps a naive question, but what is the recommended manner of
  testing a helper method in rspec?

You just describe the helper module and call the helper method like
you'd do in the views:

require File.dirname(__FILE__) + '/../spec_helper'

describe ApplicationHelper do
  describe breadcrumbs do
describe when breadcrumbs empty do
  it should show nothing do
breadcrumbs.should == 
  end
end
  end
end

  I have created a simple string manipulation
  function and I want to write some tests for it.  It it were a script then I
  would just add if __FILE__ == $0 and add the tests below that but I do not
  feel that this is the best way to handle a rails helper.

  The code (I said it was simple) :

  module ApplicationHelper

   # keycase strips leading spaces, squeezes out extra whitespace
   # between words, downshifts all and then capitalizes the first
   # character of each word.
   #
   # This method prepares descriptive strings that are used
   # as indices for lookups.  To preserve common initialisms insert
   # periods between letters.  Thus:
   #   IBM = Ibm but I.B.M. = I.B.M.
   #
   # Do not use ! methods as they return nil under certain circumstances
   #
   def keycase(value)
 value = value.to_s.strip.squeeze( ).downcase.gsub(/\b\w/){$.upcase}
   end

  end

Something like this:

require File.dirname(__FILE__) + '/../spec_helper'

describe ApplicationHelper do
  describe keycase do
describe when no dots do
  before(:each) do
@value = IBM
  end

  it should convert to lowercase do
keycase(@value).should == Ibm
  end
end
  end
end

And so on.

//jarkko


-- 
Jarkko Laine
http://jlaine.net
http://odesign.fi
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] Story testing views in isolation

2008-03-11 Thread Jarkko Laine
On Tue, Mar 11, 2008 at 2:39 PM, Zach Dennis [EMAIL PROTECTED] wrote:
  +1 for keeping view specs act as view specs and stories act as stories.

Agreed. AFAIK stories are for describing a series of actions and
behavior and that doesn't really fit in nicely with specifying views
in isolation.

//jarkko

-- 
Jarkko Laine
http://jlaine.net
http://odesign.fi
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] Webrat integration with Rspec and Rake tasks

2008-02-23 Thread Jarkko Laine

On 23.2.2008, at 4.00, Joe Van Dyk wrote:

How are you using both autotest and the stories?


I use Geoffrey's rstakeout (http://nubyonrails.com/articles/automation-with-rstakeout 
) to watch a single story (running all stories a la autotest is too  
slow) and /app, and autotest in a different terminal for normal specs.


//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi




smime.p7s
Description: S/MIME cryptographic signature
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Re: [rspec-users] spec_server requiring absolute paths (and autotest breaking)

2008-02-20 Thread Jarkko Laine


On 21.2.2008, at 4.14, Scott Taylor wrote:



On Feb 20, 2008, at 1:52 AM, Jarkko Laine wrote:


On 20.2.2008, at 6.55, Scott Taylor wrote:

That said, now that autotest runs via drb, it's vastly (like 50%)
slower than without it,


That's the idea.


Isn't the idea that specs run faster through drb, rather than
slower? However, in general my specs seem to take a lot longer when
run through autotest in the first place (compared to running just
rake spec:models, e.g.). I haven't so far been able to track down
what causes this.


Oops.  Misread your post.

Is this true running a single spec (or a single file)?  I've noticed
that a test suite which takes about 5 seconds normally takes about 7
seconds under drb (according to the numbers reported by rspec).  Then
again, running a single file is blazingly fast, as their is no wait
(as though I'm testing against a non-rails project).


If I run a single, small file (like a helper spec), it's faster in an  
absolute sense (using time):


Probutanol:koulutusweb jarkko$ time script/spec -O spec/spec.opts spec/ 
helpers/date_extension_spec.rb



Finished in 2.061605 seconds

4 examples, 0 failures

real0m2.212s
user0m0.060s
sys 0m0.024s
Probutanol:koulutusweb jarkko$ time script/spec spec/helpers/ 
date_extension_spec.rb



Finished in 0.102506 seconds

4 examples, 0 failures

real0m4.682s
user0m3.738s
sys 0m0.688s


However, as you can see even there the actual running of the specs is  
painfully slow (2 vs 0.1 secs) and the only reason drb is faster is  
because there is no startup time. This is on 3316. Also, there really  
seems to be something wonky going on with the loading. When I run a  
model spec right after starting the spec server, it works, but running  
it the second time already gives some weird stuff back:


Probutanol:koulutusweb jarkko$ time script/spec -O spec/spec.opts spec/ 
models/account_spec.rb

..

Finished in 0.371612 seconds

10 examples, 0 failures

real0m1.647s
user0m0.068s
sys 0m0.024s
Probutanol:koulutusweb jarkko$ time script/spec -O spec/spec.opts spec/ 
models/account_spec.rb

.FFF.F

1)
ActiveRecord::AssociationTypeMismatch in 'Account users should return  
user'

Address expected, got Address






Mock 'Location_18994' expected :valid? with (any args) once, but
received it twice


I saw this happen recently when the spec server was started twice:
once with the rake task (rake spec:server:start), the other with a
straight script/spec_sever.  My hunch is that both servers are
loading the files at the same time.


That would seem logical. However, I just checked that I only have
one spec_server process running and am still able to reproduce the
problem.


Hmm.  Are you reloading classes or using anonymous classes derived
from AR::Base, or anything crazy like that?  Are all your models in
app/models?  Do you have pending migrations?  Are your running with
rake, or with autotest? How about running the file alone?


Doesn't matter. Shouldn't be doing anything super special with models  
but got to think about it.



expected: Barney Hops doesn't have the required competency (MBA)
to teach this class,
   got: [Barney Hops doesn't have the required competency (MBA)
to teach this class, Barney Hops doesn't have the required
competency (MBA) to teach this class]



Yeah - this is because rails load's instead of requires.  You'll  
also

see this sort of thing if you just call load directly in your specs.
I've never dug deeper into how all of the rails magic happens
regarding loading and constants (and the method generation with the
association macros (has_* belongs_to), so if you have any insight,
I'd be happy to hear it.


Yeah, all these failures are certainly related and probably caused
by something like what you describe. Got to try to figure out what
spec_server does differently than normal spec. Or maybe we should
just bite the bullet and make the deeptest spec fork work :-)


Yeah - deeptest is great for slow suites, but now that I have a fast
suite (take 5-7 seconds), deeptest probably wouldn't be much of a
performance gain for me.


Yeah, the funny thing is, model specs that sometimes need to go to the  
db are pretty fast but controller specs where most of the db traffic  
is stubbed out seem to become a drag, and I would think deeptest would  
help there.


--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi




smime.p7s
Description: S/MIME cryptographic signature
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Re: [rspec-users] spec_server requiring absolute paths (and autotest breaking)

2008-02-19 Thread Jarkko Laine

On 19.2.2008, at 17.57, David Chelimsky wrote:

Did this just start happening for you? If so, it could be the patch I
applied in r3310. Would you mind checking out 3309 and seeing if you
still have this problem?



Ok, found out the reason. I started spec_server from within the script  
directory and it only works correctly if you start it from the root of  
your Rails app. Gotcha.


That said, now that autotest runs via drb, it's vastly (like 50%)  
slower than without it, doesn't respect the --colour option in  
spec.opts and causes failures that don't happen when running just  
plain autotest. All of them seem to be related to methods being run  
more times than they are supposed to. Examples:


Mock 'Location_18994' expected :valid? with (any args) once, but  
received it twice


expected: Barney Hops doesn't have the required competency (MBA) to  
teach this class,
 got: [Barney Hops doesn't have the required competency (MBA) to  
teach this class, Barney Hops doesn't have the required competency  
(MBA) to teach this class]


Mock 'Enrollment' expected :on_waiting_list? with (any args) twice,  
but received it 3 times


//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi




smime.p7s
Description: S/MIME cryptographic signature
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

[rspec-users] spec_server requiring absolute paths (and autotest breaking)

2008-02-19 Thread Jarkko Laine

rspec and rspec_on_rails rev 3316, ZenTest 3.9.1

When run through spec_server, running spec fails if the spec file is  
not specified with an absolute path:


Probutanol:koulutusweb jarkko$ script/spec --options spec/spec.opts  
`pwd`/spec/models/address_spec.rb

..

Finished in 0.166251 seconds

6 examples, 0 failures
Probutanol:koulutusweb jarkko$ script/spec --options spec/spec.opts  
spec/models/address_spec.rb
(druby://localhost:8989) ./../vendor/plugins/rspec/lib/spec/runner/ 
options.rb:197:in `files_to_load': File or directory not found: spec/ 
models/address_spec.rb (RuntimeError)
	from (druby://localhost:8989) ./../vendor/plugins/rspec/lib/spec/ 
runner/options.rb:189:in `each'
	from (druby://localhost:8989) ./../vendor/plugins/rspec/lib/spec/ 
runner/options.rb:189:in `files_to_load'



In the same way, autotest fails with spec_server:

Probutanol:koulutusweb jarkko$ autotest
loading autotest/rails_rspec
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -S  
script/spec -O spec/spec.opts  spec/controllers/ 
activations_controller_spec.rb spec/helpers/clients_helper_spec.rb

[a bunch of files omitted]
spec/models/enrollment_notifier_spec.rb spec/views/clients/ 
email_form.html.erb_spec.rb
(druby://localhost:8989) ./../vendor/plugins/rspec/lib/spec/runner/ 
options.rb:5:in `mtime': Not a directory - spec/models/address_spec.rb  
(Errno::ENOTDIR)
	from (druby://localhost:8989) ./../vendor/plugins/rspec/lib/spec/ 
runner/options.rb:5
	from (druby://localhost:8989) ./../vendor/plugins/rspec/lib/spec/ 
runner/options.rb:251:in `sort'
	from (druby://localhost:8989) ./../vendor/plugins/rspec/lib/spec/ 
runner/options.rb:251:in `sorted_files'



Any ideas why spec_server has lost its marbles? Without the --drb  
option autotest runs fine.


Cheers,
//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi




smime.p7s
Description: S/MIME cryptographic signature
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Re: [rspec-users] spec_server requiring absolute paths (and autotest breaking)

2008-02-19 Thread Jarkko Laine

On 19.2.2008, at 17.57, David Chelimsky wrote:

Did this just start happening for you? If so, it could be the patch I
applied in r3310. Would you mind checking out 3309 and seeing if you
still have this problem?


That's it. However, with 3309 it seems that autotest doesn't really  
run through drb at all since it runs the same whether or not the  
spec_server is running. Yeah, I know that was the reason for the patch.


//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi




smime.p7s
Description: S/MIME cryptographic signature
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Re: [rspec-users] spec_server requiring absolute paths (and autotest breaking)

2008-02-19 Thread Jarkko Laine

On 20.2.2008, at 6.55, Scott Taylor wrote:

That said, now that autotest runs via drb, it's vastly (like 50%)
slower than without it,


That's the idea.


Isn't the idea that specs run faster through drb, rather than slower?  
However, in general my specs seem to take a lot longer when run  
through autotest in the first place (compared to running just rake  
spec:models, e.g.). I haven't so far been able to track down what  
causes this.



Mock 'Location_18994' expected :valid? with (any args) once, but
received it twice


I saw this happen recently when the spec server was started twice:
once with the rake task (rake spec:server:start), the other with a
straight script/spec_sever.  My hunch is that both servers are
loading the files at the same time.


That would seem logical. However, I just checked that I only have one  
spec_server process running and am still able to reproduce the problem.



expected: Barney Hops doesn't have the required competency (MBA)
to teach this class,
got: [Barney Hops doesn't have the required competency (MBA)
to teach this class, Barney Hops doesn't have the required
competency (MBA) to teach this class]



Yeah - this is because rails load's instead of requires.  You'll also
see this sort of thing if you just call load directly in your specs.
I've never dug deeper into how all of the rails magic happens
regarding loading and constants (and the method generation with the
association macros (has_* belongs_to), so if you have any insight,
I'd be happy to hear it.


Yeah, all these failures are certainly related and probably caused by  
something like what you describe. Got to try to figure out what  
spec_server does differently than normal spec. Or maybe we should just  
bite the bullet and make the deeptest spec fork work :-)


Cheers,
//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi




smime.p7s
Description: S/MIME cryptographic signature
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Re: [rspec-users] Webrat integration with Rspec and Rake tasks

2008-02-19 Thread Jarkko Laine


On 19.2.2008, at 20.45, James Deville wrote:


I set RAILS_ENV in my stories/helper.rb file. That might be a good
solution.


On Feb 19, 2008, at 10:32 AM, Ed Howland wrote:


Hi,

I am trying to get Webrat integrated with RSpec. I've followed the
steps in the various blog posts about this, but have one sniggling
little issue. It might be a Webrat issue, but I am hoping the RSpec
folks might have already solved it.

I have the stories run from a Rake task  (am hoping for one of these
in the main rspec rake task soon,) ut to run webrat stuff in the
steps, you need to:

rake spec:stories RAILS_ENV=test.

The reason is due to the boot order of rake and plugins (of which
webrat and rspec are ones), webrat/init.rb doesn't attach itself to
ActionController::Integration::Session unless it is on the test
environment. Only setting the RAILS_ENV ahead of the boot sequence
fixes this. But I am wondering if there is another alternative.

Also, has anyone gotten it to run with autotest?


Using Webrat for 100% of my stories (and running autotest) and loving  
it. I also have the env set in stories/helper.rb, but I'm not sure  
whether it was there from the beginning (I just copied it from some of  
the early posts about stories).


//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi




smime.p7s
Description: S/MIME cryptographic signature
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Re: [rspec-users] deep test with rspec?

2008-02-19 Thread Jarkko Laine


On 20.2.2008, at 7.12, Ben Mabey wrote:


Scott Taylor wrote:

On Feb 19, 2008, at 4:44 PM, Brian Takita wrote:



On Feb 18, 2008 7:06 PM, Dan Manges [EMAIL PROTECTED] wrote:


David Vollbracht spiked an rspec implementation for DeepTest:
svn diff -r 20:21 http://deep-test.rubyforge.org/svn/trunk

Unfortunately nobody has had time to finish it. If anybody is
interested in
working on it, that would be awesome. I can give you repo access.


I would be interested in trying to get it to work.



Cool.  Glad to hear it's out there.  I'd be interesting in helping
out, too.

Scott

___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


I'd like to see this working as well.  I have never used rinda so I'll
check it out so see what I can do to help.


+1




-Ben
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi




smime.p7s
Description: S/MIME cryptographic signature
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Re: [rspec-users] Example controller spec no worky?

2008-02-13 Thread Jarkko Laine


On 13.2.2008, at 21.09, Brad Carson [EMAIL PROTECTED] wrote:

 Pat Maddox wrote:
 Try

 Event.stub!(:find).and_raise(RecordNotFound)

 To my credit, I did try that (though ActiveRecord::RecordNotFound, of
 course) but then I get:

 EventsController attempt to show non-existing event
 - should render 404 (ERROR - 2)
 - should not assign an event (ERROR - 3)

 ActiveRecord::RecordNotFound in 'EventsController attempt to show
 non-existing event should render 404'
 ActiveRecord::RecordNotFound

 Mind-boggling.

The exception is never rescued. You'd have to catch the exception  
yourself, I think, and render the 404 page. I might be talking through  
my ass but I think the built in rescue of AR::RNF only works in  
production.

//jarkko


 -- 
 Posted via http://www.ruby-forum.com/.
 ___
 rspec-users mailing list
 rspec-users@rubyforge.org
 http://rubyforge.org/mailman/listinfo/rspec-users
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] Rspec accessing the wrong id?

2008-02-07 Thread Jarkko Laine

On 6.2.2008, at 11.16, David Currin wrote:

 Hi,

 This is probably straightforward to solve. I'm new to rspec so I hope
 you'll be patient with me. I've created a model method which works in
 practice, but which rspec is not passing. I'm obviously not going
 about things in the right way in rspec so I'd appreciate any advice
 offered.

 Here's the (releveant parts of the) method which sets up a one to many
 relationship between Company and Projects.

 def.self create_project(record)
parent_company = Company.find_by_basecampid(record.company.id)
project = self.find_or_initialize_by_basecampid(record.id)
project.company_id = parent_company.id
 end

 In rspec I've got this:

 def parent_company_attributes
{
  :id = 1,
  :basecampid = 448718,
  :name = The Media Collective
}
  end

 before do

 mock_record
 Company.should_receive(:find_by_basecampid).
with(parent_company_attributes[:basecampid]).
and_return(parent_company_attributes)

You're returning a hash here. If you call id for hash (like you do in  
this case), it will return the object id, not the element with key :id.

I think what you want to do is to create a mock_model with that hash  
and then stub Company.find_by_basecampid to return that mock object.

//jarkko



 @project = Project.create_project(record)

 end

 it should populate company_id do
@project.company_id.should eql(parent_company_attributes[:id])
 end


 The problem is that it looks like rspec is using the object id rather
 than the id from parent_company_attributes. Here's the failure
 message:

 1)
 'Project.create project record should populate company_id' FAILED
 expected 1, got 8817400 (using .eql?)

 Thanks for any insights

 David Currin
 ___
 rspec-users mailing list
 rspec-users@rubyforge.org
 http://rubyforge.org/mailman/listinfo/rspec-users

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] Can you explain this code to me?

2008-02-07 Thread Jarkko Laine
On 8.2.2008, at 1.26, Pito Salas wrote:

 I am experimenting with the restful-authentication plug in and I see
 that it has generated an rspec encantation which I had not seen
 before:

  it 'allows signup' do
lambda do
  create_user
  response.should be_redirect
end.should change(User, :count).by(1)
  end

 What does the combination of a lambda and a .should mean?

Lambda is an anonymous function, or pretty much the same as a block of  
code. So in the case above, running the code inside the lambda block  
should change the user count by one. This is the same as  
assert_difference does in test/unit.



 Similarly but a little different:

  describe 'being created' do
before do
  @user = nil
  @creating_user = lambda do
@user = create_user
violated [EMAIL PROTECTED] if
 @user.new_record?
  end
end

it 'increments User#count' do
  @creating_user.should change(User, :count).by(1)
end
  end

 Thanks for any pointers or clarifications!

Same thing, just this time the function is first stored in an instance  
variable.

//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] RESTful Routes

2008-02-04 Thread Jarkko Laine

On 4.2.2008, at 14.54, Raimond Garcia wrote:

 Hi,

 I'm trying to test the behavior of a route with a delete method.

 Something like this:
 When the admin clicks on 'delete' do
 delete admin_blog_path(@blog)
   end

 which raises the following exception:
 NoMethodError: You have a nil object when you didn't expect it!
 The error occurred while evaluating nil.env


 If I change the delete, for a get or a post it works like a charm.

 What am I missing here?

I would recommend against using the restful named routes in your  
stories. You want to test your full stack there, right, so just use  
the url. I also recommend using Webrat in conjunction with stories: 
http://www.benmabey.com/2008/02/04/rspec-plain-text-stories-webrat-chunky-bacon/

That way you are much more testing the behaviour of the application  
rather than its implementation.

If you only want to test that the route is working, it would imho  
belong to the controller specs, not to the stories.

Cheers,
//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] Not seeing the failure

2008-01-23 Thread Jarkko Laine
On 23.1.2008, at 18.02, Corey Haines wrote:

 I get the unexpected message :user= failure.

 When you set up the mock with the initial parameters, wouldn't those  
 be stubbing: @coupon.stub!(:user).and_return(@current_user)


You can do it like this:

@coupon = mock_model(Coupon, :user= = nil, :user =  
@current_user, :save = true)

That stubs both user= and user. I often create reusable mock  
factories in spec_helper.rb that take care of the normal methods for  
AR objects:

def mock_coupon(opts = {})
   mock_model(Coupon, {:user= = nil, :user = mock_user, :save =  
true}.merge(opts))
end

Often I also stub the errors method there because it's called pretty  
often in controllers and views.

//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] Not seeing the failure

2008-01-23 Thread Jarkko Laine

On 23.1.2008, at 18.57, Corey Haines wrote:

 Nope, that still didn't work.
 Still getting
 @coupon.stub!(:user=).with(@current_user)

 Here's my spec and code using your recommended way:

 spec - http://pastie.caboo.se/142451
 code - http://pastie.caboo.se/142452

 If I add back
 @coupon.stub!(:user=).with(@current_user)

 they pass.

 It seems as though the understanding is that setting up your mock  
 with the options (:user = @current_user) should set stubs for both  
 get and set, but it doesn't. I think it just sets up the stub for  
 the get.

I didn't say it would (and it doesn't and shouldn't):
 You can do it like this:

 @coupon = mock_model(Coupon, :user= = nil, :user =
 @current_user, :save = true)

Note both :user= and :user.

In your pastie you only have :user

Cheers,
//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] specs on private methods

2008-01-11 Thread Jarkko Laine


On Jan 11, 2008, at 5:54 PM, Ben Mabey [EMAIL PROTECTED] wrote:

 David Chelimsky wrote:


 In TDD there is a rule of thumb that says don't stub a method in the
 same class as the method you're testing. The risk is that as the real
 implementation of by_input_sets!() changes over time, it has access  
 to
 internal state that could impact the behaviour of decompose!().



 So, stubbing a current_user method on a rails controller would be
 considered bad practice?
 I suppose stubbing the find on User would be just as easy but I have
 always just stubbed controller.current_user.


In my login_as helper, I just call controller.send(:current_user=,  
user). No need to stub anything.

//jarkko

 -Ben
 ___
 rspec-users mailing list
 rspec-users@rubyforge.org
 http://rubyforge.org/mailman/listinfo/rspec-users
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] specs on private methods

2008-01-10 Thread Jarkko Laine

On 10.1.2008, at 22.21, Francois Wurmus wrote:

 Apart from private or public methods I see a problem in your test  
 case.
 You don't make sure that your mac_address is returned in exactly the  
 way
 you want - you are merely saying it should look like the return  
 value of
 some (protected) method you call (normalize_mac).

 Let's assume this method was implemented wrong and just returns nil.
 Let's further assume that @signup.mac_address calls a reader method
 returning nil, too. Such an implementation, though wrong, would  
 satisfy
 your spec!

 A second problem is, that the parameter you are passing to the
 normalize-method will already have been transformed before your method
 gets its hands on it. You are not passing the init value but the value
 that is returned be a read operation on the mac_address field.

 What you really should specify is your concrete mac address format:

it should return a valid mac address do
   @signup.mac_address = 00-11-22-33-44-55
   @signup.mac_address.should == 00:11:22:33:44:55
end

 Now you are not only implementation independent but your spec is also
 saying more.

Also, this line of code is a bit smelly:

 normalized = @signup.normalize_mac(@signup.mac_address)

Since normalize_mac is an instance method in the Signup class, there's  
no point passing the mac_address as a parameter; the method can simply  
call the mac_address method directly. If you want to make it more  
general-purpose, it doesn't sound like it should be an instance method  
anymore.

//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] params not available for controller specs?

2007-12-04 Thread Jarkko Laine

On 4.12.2007, at 10.17, Al Chou wrote:

 Hi, all,

 I'm trying to write a spec for a controller method that starts out:


   def download
 @orders = Order.find( params[:ids] )
 ...

 and started writing a spec that set params[:ids] to a mock.

Why would you want to set params[:ids] to a mock? params values are  
always basically strings (or hashes/arrays of strings) and you can set  
them in the actual action call:

get :foo, :ids = [1,2,3]

Moreover, in your case Order.find should be the thing you want to  
stub. You don't want the finder call to go to the database, since  
you're speccing the controller behaviour here, not business logic.

//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] params not available for controller specs?

2007-12-04 Thread Jarkko Laine

On 4.12.2007, at 23.11, Al Chou wrote:

 I think Jarkko is saying that no spec should have been written for  
 the code I was trying to fix/change.  I'm not sure I would agree  
 that BDD/RSpec is an inappropriate tool for documenting what I was  
 trying to change, but I think he would argue that

What I was saying was that I think it's important that Order.find gets  
called with an array of ids. That's what you want to happen and to  
have in your spec, right? So in your spec, you'd have

it should find orders with an array of ids do
   Order.should_receive(:find).with([1, 2, 3])
   get :download, :ids = 1/2/3
end

(or whatever ids you pass to the action).

Now, if the current, buggy behaviour just passes the string as 1/2/3  
to Order.find, the spec above will break, because it will not receive  
an array but a string. After you add the split(/) call to the actual  
code, your broken spec is fixed, and the code works as expected. Tada!  
No need to stub String#split anywhere.

//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] Need help mocking this out

2007-12-03 Thread Jarkko Laine

On 3.12.2007, at 11.57, Fischer, Daniel wrote:

 Let's say you're using the restful_authentication plugin.

 You have a model called articles. On the index action of the  
 articlescontroller you simply want to spec out that it'll scope the  
 results to the ownership of the current_user.

 It should NOT include any articles other than the articles that user  
 owns.

Given that you have

user has_many :articles

You want to use current_user.articles (or  
current_user.articles.find(*additional options*)).

In the spec, you want to make sure that the articles are scoped  
through the correct user:

before(:each) do
   @user = mock_model(User)
   User.stub!(:find).and_return(@user) # to make sure current_user is  
the mock model
   @articles = [mock_model(Article)]
end

it should find articles for the current user do
   @user.should_receive(:articles).and_return(@articles)
   get :index
end

It is then the responsibility of the articles association to scope the  
results. In the controller specs you just trust that it works.

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] spec for model_id should eql

2007-12-03 Thread Jarkko Laine

On 3.12.2007, at 21.05, Nathan Sutton wrote:

 Department.stub!(:find_by_code).with(75).and_return(3)

You can't use with() with stub! (only with should_receive), so just  
leave it away:

Department.stub!(:find_by_code).and_return(3)

//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] Need help mocking this out

2007-12-03 Thread Jarkko Laine

On 4.12.2007, at 8.40, Fischer, Daniel wrote:

 Sorry for so many messages, I hope I don't get in trouble for this.  
 Maybe IRC would be better if there was a RSpec one.

#rspec @freenode

//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] Stories and Pending Actions

2007-11-21 Thread Jarkko Laine

On 20.11.2007, at 10.08, Nathan Sutton wrote:

 Didn't have time tonight, stand-by for something tomorrow.

Done: http://rspec.lighthouseapp.com/projects/5645-rspec/tickets/132- 
plain-text-stories-should-support-given-and-given

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


[rspec-users] Testing transactional updates in story runner

2007-11-21 Thread Jarkko Laine
How do you guys test transactional updates with story runner?

I have an action that needs to update four different models:

 Enrollment.transaction do
   @contact.update_addresses(params[:contact_address], params 
[:billing_address])
   @enrollment.update_attributes!(params[:enrollment])
 end

The last update_attributes! will raise ActiveRecord::RecordNotValid  
if anything was invalid (it cascades to contact and further to  
addresses), and thus cause a rollback, so either all the updates go  
through or none of them. This works fine in development and production.

However, in story runner  the whole story is put inside a big  
transaction and further attempts to create or rollback transactions  
are ignored. This is of course nice from the performance point of  
view, but if you have code that depends on actual transactions in the  
app, you're screwed. In my example the addresses would stay updated  
in the story runner's eyes even though in real life the changes would  
be rolled back, thus making stories not what they're supposed to be,  
end-to-end tests for the application stack.

Has anyone found an elegant way to circumvent this with story runner?

Cheers,
//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] confirming that a model instance was correctly created from POST params

2007-11-20 Thread Jarkko Laine

On 20.11.2007, at 8.00, Al Chou wrote:
 What I am trying to demonstrate to myself is that the addition of a  
 boolean is_business_address column to the order_addresses table  
 via a migration allows line 40 of the create_order_from_post method  
 to set this property when an OrderAddress instance is created.  But  
 my example as shown tells me that assigns[:billing_address] is nil  
 when I run it.  Also, I realize that line 16 of the example isn't  
 returning the right thing; it should be an AssociationCollection of  
 OrderAddress.  That's where I'm starting to wonder if I'm spending  
 too much time on something that is probably simple enough that it's  
 already working but disproportionately hard to write an RSpec  
 example for (because the existing create_order_from_post method has  
 no tests or examples and thus isn't easy to test -- witness the 32  
 lines, modulo blank lines, of stubbing just to get the example to  
 run).

 Suggestions on how to write the example, or advice on whether to  
 continue to try, are requested.

A couple of points:

* I have a similar situation with shipping and billing addresses. I  
solved it by adding attr_accessor :single_address to the customer  
model. That gives you the option of using check_box :single_address  
in the form for the customer and it will automatically be passed to  
the model when you update its attributes. I've noticed it cleans up  
the controller code a lot.
* for line 16 in the spec, you need to do something like this:

order_addresses = []
order_addresses.stub!(:create).and_return(order_address) #  
order_address must be created before this
order_user.stub!( :order_addresses ).and_return( order_addresses )

* You should put all the stubbing and mocking in a before(:each)  
block, they don't really belong to the actual spec.
* What you have in your helper is basically business domain stuff.  
Helpers in Rails are meant for streamlining view code. I just  
refactored some code that does similar things that yours does, and  
noticed that it fits perfectly in the model. So in your case, you  
could add e.g. a class method Order.create_with_addresses_and_user 
(params[:order], params[:order_user], params[:billing_address], params 
[:shipping_address], params[:order_account]). In that case, all you'd  
have to spec in the controller is that the class method is called  
with correct params. All the other spec'ing would go into the model's  
specs.
* Unless your db supports nested transactions (very few do), there's  
no point in putting nested transaction blocks in your code.
* You can shorten your mocks a bit by defining the stubbed methods  
already in the mock_model call:

order_account = mock_model( OrderAccount, :valid? = true,

:order_id= = true,

:save! = true )

and so on. Also, once your specs are more than just the one, you  
start finding patterns there and you can easily refactor the stubbing  
into spec helpers so that you only need a line or two to create a  
comprehensive mock model in your specs.

HTH,
//jarkko

P.S. in Ruby true and false are normally written in lower case.

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] How to represent Story of Life grid in Then clause

2007-11-20 Thread Jarkko Laine
On 20.11.2007, at 3.44, Ed Howland wrote:

 In this story from Dan's Game of Life:

 Scenario: the grid has three cells
 Given 3 x 3 grid
 When I create a cell at 0, 0
 And I create a cell at 0, 1
 And I create a cell at 2, 2
 Then the grid should look like:
   XX.
   ...
   ..X

 How do you set up matchers for the grid at the end.  Are all the
 indented lines after a Then passed to the Then matcher as separate
 strings with multiple Then()s getting called or a single string
 separated by new lines?

No, they aren't (see http://rspec.lighthouseapp.com/projects/5645/ 
tickets/132-plain-text-stories-should-support-given-and-given).  
However, multiline parameters aren't supported in plain text story  
steps yet, either.

//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] How to test views with Nested Resources and Partials

2007-11-20 Thread Jarkko Laine
Hi Troy,

On 20.11.2007, at 22.21, Troy Nini wrote:
  Output from running edit.haml_spec.r 

 F

 1)
 ActionView::TemplateError in '/line_items/edit.haml should render  
 edit form'
 contract_line_item_url failed to generate from  
 {:contract_id=1, :action=show, :controller=line_items},  
 expected: {:action=show, :controller=line_items}, diff:  
 {:contract_id=1}
 On line #6 of app/views/line_items/edit.haml

 3:
 4: #section1
 5:   = error_messages_for :line_item
 6:   - form_for(:line_item, :url = contract_line_item_path 
 (@contract.id)

contract_line_item_path (and _url) expects two parameters, contract  
and line item (or their ids). The errors the routing code outputs are  
often pretty cryptic.

//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] Spec slowdown

2007-11-14 Thread Jarkko Laine

On 13.11.2007, at 19.21, David Chelimsky wrote:

 On Nov 13, 2007 10:52 AM, Jarkko Laine [EMAIL PROTECTED] wrote:
 Is there something currently going on on the rspec trunk that causes
 a massive slowdown when running rake spec and autotest (without
 spec_server)? I updated to the latest trunk yesterday and the time
 needed to run the specs of my app jumped from ~20 seconds to more
 than a minute. The weird thing is that if I run the rake spec tasks
 separately (controllers, models, views, helpers), they still take
 about 20 seconds in total: http://pastie.caboo.se/117362

 That's really weird. We are doing some refactoring and it is
 admittedly still mid-stream, but I'm stumped as to why it would only
 have a negative impact when you run all the examples.

 Also - I'm using the latest and do not see this drop in speed.

Interesting. This happens both on my laptop and the CI server,  
though, so it can't just be my box.


 So, for the moment, please keep an eye on it and let us know if
 changes over the next few days resolve this or not. If not, we'll take
 a closer look before the release.

Thanks, I'll keep you posted.


 Thanks,
 David

 ps - in case you didn't notice: config.breakpoint_server has been
 deprecated and has no effect.

:-)

//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


[rspec-users] Spec slowdown

2007-11-13 Thread Jarkko Laine
Is there something currently going on on the rspec trunk that causes  
a massive slowdown when running rake spec and autotest (without  
spec_server)? I updated to the latest trunk yesterday and the time  
needed to run the specs of my app jumped from ~20 seconds to more  
than a minute. The weird thing is that if I run the rake spec tasks  
separately (controllers, models, views, helpers), they still take  
about 20 seconds in total: http://pastie.caboo.se/117362

//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] Examples of writing controller specs that use authentication

2007-10-27 Thread Jarkko Laine

On 27.10.2007, at 8.54, Pat Maddox wrote:

 On 10/26/07, Leslie Freeman [EMAIL PROTECTED] wrote:
 Hello,
 I'm working on specs for a controller that handles authentication
 using the restful_authentication plugin. I'm trying to find a
 resource (tutorial or examples, if possible) about the best way to go
 about writing mocks and specs to make sure that things like my
 before_filters are working correctly. Does anyone know of any good
 resources for this?

 Thanks,
 Les
 ___
 rspec-users mailing list
 rspec-users@rubyforge.org
 http://rubyforge.org/mailman/listinfo/rspec-users


 To make sure that the filter works when you're not logged in, just
 make the request:

 describe CategoriesController,  POST /categories, not logged in do
   it should redirect to the login url do
 post :create, :category = {name = foo}
 response.should redirect_to(login_url)
   end
 end

 and when you want to spec the meat of the request, stub the  
 logged_in? method:

Or, if you need different users to behave in different ways, you  
might want to create a helper in spec_helper.rb, such as this:

   def login_as(user, options = {:id = 5, :needs_activation = false,
 :admin = false})
 user.stub!(:id).and_return(options[:id])
 user.stub!(:to_param).and_return(options[:id])
 user.stub!(:needs_activation?).and_return(options 
[:needs_activation])
 user.stub!(:admin?).and_return(options[:admin])
 controller.send :current_user=, user
   end

Then you can just say this in the specs:

login_as(@user_mock_object)

and the controller will behave accordingly (assuming you use  
restful_authentication/acts_as_authenticated).

//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] Story Runner, autoincrementing

2007-09-23 Thread Jarkko Laine
Sorry for top posting, this is all I get from the mobile gmail.

You don't specify the id of the post when you create one. This is the
same for all restful create actions. In case of nested routes, you
give the parent resource as a param, with normal routes nothing:

post forum_posts_path(@forum)

Or

post /forums/1/posts/

That said, can't you also save the id when you create the object in a
given block?

//jarkko



On 9/23/07, Andrew WC Brown [EMAIL PROTECTED] wrote:
 I've written a story and I run into a snag.
 I've written the create_forum method and told it to set the id to 1
 but when it creates the forum the id is autoincremented.
 My forums table is empty but the id keeps incrementing the newest record on
 creation.
 When I run the story it comes out as something like 59 which fails my story
 because I'm asking it to look at /forums/1

 I thought of using my named routes instead of specifying the path as a
 string.
 That worked up until the point when I reached:

 When creating a new topic titled, Nicks Mom do |title|
   post topics(@forum, ??? ), :topic = { :title = title, :body = She is
 teh hotZ! }
 end

 The problem was since its a nested routed I couldn't complete the post
 request without specifying the topic_id.
 But since the topic hasn't been created yet their is no way (that I know of)
 to pass the id since I don't know what it will be.

 I would think the better practice is to state the paths as strings instead
 of using the nested routes.



 require File.join(File.dirname(__FILE__), helper)

 Story User creates a new topic, %{
 As a user
 I want to create a new topic
 So that I can dicuss Nick's Mom
   }, :type = RailsStory do

   Scenario Successfully create a new topic in the 'General' forum do

 Given a user named, Jon do |login|
   @user = create_user login
 end
 Given a forum named, General do |name|
   @forum = create_forum name
   puts @forum.id
 end

 And user logged in successfully and was redirect to, / do |path|
   post /session/create, :login = Jon, :password = your_momma
   response.should redirect_to(/)
 end

 And user is looking at, /forums/1 do |path|
   get path
   response.should be_success
 end

 When creating a new topic titled, Nicks Mom do |title|
   post /forums/1/topics/1/create, :topic = { :id = 1, :title =
 title, :body = She is teh hotZ! }
 end

 Then user should be redirected to, /forums/1/topics/1 do |path|
   get path
   response.should be_success
 end

   end

   Scenario Failed creating a new topic due to blank fields do; end
   Scenario Sticky a new thread do; end
   Scenario Annoucment as a new thread do; end
 end

 def create_user(login)
   user = User.create!(
 :login = login,
 :email = [EMAIL PROTECTED],
 :password = your_momma,
 :password_confirmation = your_momma,
 :display_name = Jon)
   user
 end

 def create_forum(name)
   forum = Forum.create!(
 :id = 1,
 :name = name,
 :description = Everything from the latest gossip to the coolest
 youtube videos.)
   forum
 end


 How do I stop the incrementing?



-- 
--
Jarkko Laine
http://jlaine.net
http://odesign.fi
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] Failing to raise an exception in a stub

2007-09-14 Thread Jarkko Laine

On 13.9.2007, at 23.29, aslak hellesoy wrote:


On 9/13/07, Jarkko Laine [EMAIL PROTECTED] wrote:

I've come across rather strange behaviour when trying to raise an
exception in a stubbed method.

I'm speccing the behaviour of a Rails create action, where I'm using
save! to catch failed saves. In the case of working save, I'm using
the following stub:

 @client.stub!(:save!).and_return(true)

which works fine.

However, in the negative case,

 @client.stub!(:save!).and_raise(ActiveRecord::RecordInvalid)



Passing a class only works if the new method takes 0 args. Otherwise
you have to pass an exception instance. ActiveRecord::RecordInvalid
takes one.

@client.stub!(:save!).and_raise(ActiveRecord::RecordInvalid.new 
(@client))


Cheers, that was it!

//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi




smime.p7s
Description: S/MIME cryptographic signature
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Re: [rspec-users] Philosophical questions

2007-09-14 Thread Jarkko Laine

On 14.9.2007, at 1.10, Pat Maddox wrote:

Others have mentioned Story Runner, which is a new feature of RSpec.
I'm pretty jazzed about it, because it's a very lightweight tool for
acceptance tests.  The structure is clean, it just makes sense to me.
I wrote an introduction which you can find at
http://evang.eli.st/blog/2007/9/1/user-stories-with-rspec-s-story- 
runner.

 It also has links to some of Dan North's articles which are a great
resource for really getting BDD.


+1 for Story Runner. I just installed it on a project for a client  
that were using controller specs heavily for end-to-end stuff. Even  
on an app running on Rails 1.2, the rspec trunk installed flawlessly  
with some minimal changes to the existing spec code. From what I  
learned, Story Runner is already highly usable even though it's not  
officially out.


//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi




smime.p7s
Description: S/MIME cryptographic signature
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Re: [rspec-users] Testing a nested controller

2007-09-14 Thread Jarkko Laine


On 14.9.2007, at 12.02, Andreas Wolff wrote:


Hey everyone.
I really stuck on testing a nested controller. I'm trying to make a
request using get and afterwards checking the response by
response.should ...

My routes.rb looks like this:

map.resources :writers do |writers|
 writers.resources :notes
end


In my notes_controller_spec.rb

def do_get
 writer_id = 1
 note_id = 1
 get note_path(writer_id, note_id)
end

it should show a note do
 do_get
 response.should be_success
end


But this always ends in an error message:
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.rewrite

Can anybody help here??


You shouldn't test the routing stack in controller tests. Just say

get :show, :id = 1, :writer_id = 1

It will automatically use the correct controller, given that you have  
either used describe NotesController or use the controller helper  
to identify the controller. I think the former way is now considered  
best practice.


If you want to test routes, do it separately. IIRC generating an  
rspec_scaffold will create you examples of how to do that.


//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi




smime.p7s
Description: S/MIME cryptographic signature
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

[rspec-users] Failing to raise an exception in a stub

2007-09-13 Thread Jarkko Laine
I've come across rather strange behaviour when trying to raise an  
exception in a stubbed method.


I'm speccing the behaviour of a Rails create action, where I'm using  
save! to catch failed saves. In the case of working save, I'm using  
the following stub:


@client.stub!(:save!).and_return(true)

which works fine.

However, in the negative case,

@client.stub!(:save!).and_raise(ActiveRecord::RecordInvalid)

The save! call in the controller doesn't seem to work. I get the  
following error:


ArgumentError in 'ClientsController POST /clients with invalid  
parameters should show new form again'

wrong number of arguments (0 for 1)
/Users/jarkko/Sites/koulutusweb/app/controllers/clients_controller.rb: 
41:in `create'


The line #41 consists only of

@client.save!

I'm at a loss seeing where the wrong number of arguments is really  
happening because save! certainly shouldn't assume any args.  
Moreover, both cases work fine in the real app, so it seems to me  
something funky is happening when stubbing the method.


Anyone else stumbled upon anything similar?

Cheers,
//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi




smime.p7s
Description: S/MIME cryptographic signature
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Re: [rspec-users] Authenticating before tests

2007-09-13 Thread Jarkko Laine


On 13.9.2007, at 19.52, sinclair bain wrote:


Hi!

Have you considered mocking / stubbing to test in isolation if that  
is appropriate ?


Right, my example was also assuming that you are using mocks for users.

//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi




smime.p7s
Description: S/MIME cryptographic signature
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users