Way to access app/helpers methods within steak/rspec?

204 views
Skip to first unread message

David Kahn

unread,
Feb 28, 2011, 7:17:51 PM2/28/11
to ste...@googlegroups.com
I have a failing test because my number formatting in the spec is different than on the view. The current spec line:

has_text?("$#{@account.bill.grand_total}").should == true

However in my view the formatting is handled by a helper, and for me to get my test to pass I would need to do:

has_text?("$#{format_qb_amount(@account.bill.grand_total)}").should == true

I have tried this before but not successfully, is there a way to include the app/helpers in my acceptance (steak) specs?

I have tried

include BillingRunsHelper .... as I thought would be necessary to include any module, and it loads but I can not access the method. Am I missing something simple?

Thanks,

David

Luismi Cavallé

unread,
Feb 28, 2011, 7:33:48 PM2/28/11
to ste...@googlegroups.com
The reason it's hard to do what you're trying is because, as a general rule, it is not a good practice to include and use parts of your implementation in your specs, since they'd get coupled to your implementation and you wouldn't be really testing those parts of your code (in this example, your helpers and your models).

You may probably want to rewrite your spec to something along these lines:

.should have_text("$20.30")

which is much more easier to understand and doesn't depend on any part of your implementation.

-- Luismi

David Kahn

unread,
Mar 1, 2011, 10:32:27 AM3/1/11
to ste...@googlegroups.com
On Mon, Feb 28, 2011 at 6:33 PM, Luismi Cavallé <lmca...@gmail.com> wrote:
The reason it's hard to do what you're trying is because, as a general rule, it is not a good practice to include and use parts of your implementation in your specs, since they'd get coupled to your implementation and you wouldn't be really testing those parts of your code (in this example, your helpers and your models).

You may probably want to rewrite your spec to something along these lines:

.should have_text("$20.30")

which is much more easier to understand and doesn't depend on any part of your implementation.

Yeah, thanks, that is actually what I ended up doing -- the reason I was doing dynamically was that this figure is a moving target while the business logic is under development, but I wanted to verify that the number that the model generates gets out to the page. If I wanted to format the number dynamically in the test then I would have to duplicate the code from the helper which to me does not seem right and is not DRY. I understand what you are saying in general, but is still weird to me why in principle it should be so difficult to mix in a module to a test suite --- or in fact to other code. I don't expect a response, but to throw out there, is there something special about helper modules which make them different than say a module in lib/*?
 
Reply all
Reply to author
Forward
0 new messages