From dbe8a81ca7d4d9ae87b4b62926a0ba6379397fbc Mon Sep 17 00:00:00 2001 From: Carol Nichols Date: Fri, 5 Apr 2013 10:51:28 -0400 Subject: [PATCH 1/2] Document how to stub route helper functions in isolated tests. Addresses #506. --- README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/README.md b/README.md index 5c2fe07..d395029 100644 --- a/README.md +++ b/README.md @@ -390,6 +390,27 @@ Draper::ViewContext.test_strategy :fast do end ``` +#### Stubbing route helper functions + +If you are writing isolated tests for Draper methods that call route helper +methods, you can stub them instead of needing to include Rails. + +To get access to the Draper `helpers` in your test, include them in your tests: + +```ruby +describe YourDecorator do + include Draper::ViewHelpers +end +``` + +Then you can stub the specific route helper functions you need using your +preferred stubbing technique (this example uses mocha): + +```ruby +helpers.stubs(users_path: '/users') +``` + + ## Advanced usage ### Shared Decorator Methods From 943f25bdef1a2d0cff8538233a62a5fdd1d9f143 Mon Sep 17 00:00:00 2001 From: Carol Nichols Date: Fri, 5 Apr 2013 13:54:49 -0400 Subject: [PATCH 2/2] Improve documentation about stubbing route helpers * Get rid of an unnecessary newline * Change 'include Rails' to 'require Rails' * Clarify when you would need to explicitly include ViewHelpers * Switch from mocha `stubs` to RSpec `stub` --- README.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d395029..cf6f484 100644 --- a/README.md +++ b/README.md @@ -393,9 +393,12 @@ end #### Stubbing route helper functions If you are writing isolated tests for Draper methods that call route helper -methods, you can stub them instead of needing to include Rails. +methods, you can stub them instead of needing to require Rails. -To get access to the Draper `helpers` in your test, include them in your tests: +If you are using RSpec, minitest-rails, or the Test::Unit syntax of minitest, +you already have access to the Draper `helpers` in your tests since they +inherit from `Draper::TestCase`. If you are using minitest's spec syntax +without minitest-rails, you can explicitly include the Draper `helpers`: ```ruby describe YourDecorator do @@ -404,13 +407,12 @@ end ``` Then you can stub the specific route helper functions you need using your -preferred stubbing technique (this example uses mocha): +preferred stubbing technique (this example uses RSpec's `stub` method): ```ruby -helpers.stubs(users_path: '/users') +helpers.stub(users_path: '/users') ``` - ## Advanced usage ### Shared Decorator Methods