回答
你只需要包括相關的輔助模塊測試,以使可用的方法:
describe "foo" do
include ActionView::Helpers
it "does something with a helper method" do
# use any helper methods here
這真是那樣簡單。
我假設你正在測試輔助方法。爲了做到這一點,你必須把你的spec文件放入spec/helpers/
。鑑於您使用的是rspec-rails
寶石,這將爲您提供一個helper
方法,允許您調用任何輔助方法。
有在the official rspec-rails
documentation是一個很好的例子了:
require "spec_helper"
describe ApplicationHelper do
describe "#page_title" do
it "returns the default title" do
expect(helper.page_title).to eq("RSpec is your friend")
end
end
end
Nop,有很多關於測試助手方法的答案。我正在嘗試在集成測試中使用輔助方法。 Tks無論如何 –
噢好吧。如果你可以擴展你的問題並給出更多的細節(也許一些代碼示例),我會盡力幫助你:) –
如果你想使用一個輔助方法對你的看法測試,你可以用下面去:
before do
view.extend MyHelper
end
它必須是一個describe
塊內。
它爲我在軌道上3.2和RSpec 2.13
如果我們假設你在spec/helpers/my_helper_spec .rb從哪裏來的「視圖」在spec? –
@IvailoBardarov對不起,這是爲了使用內部視圖。作爲OP sais:'爲了澄清,我沒有試圖測試一個輔助方法。 – fotanus
對於任何人遲到了這個問題,它回答的Relish網站。
require "spec_helper"
describe "items/search.html.haml" do
before do
controller.singleton_class.class_eval do
protected
def current_user
FactoryGirl.build_stubbed(:merchant)
end
helper_method :current_user
end
end
it "renders the not found message when @items is empty" do
render
expect(
rendered
).to match("Sorry, we can't find any items matching "".")
end
end
正如你可以在這裏看到https://github.com/rspec/rspec-rails,你應該初始化與投機/目錄(其中規格將駐留):
$ rails generate rspec:install
這將產生一個rails_helper.rb與選項
config.infer_spec_type_from_file_location!
最後需要在你的helper_spec.rb中添加新的rails_helper,而不是需要'spec_helper'。
require 'rails_helper'
describe ApplicationHelper do
...
end
祝你好運。
基於Thomas Riboulet's post on Coderwall:
在你規範文件的開頭補充一點:
然後再打一個特定助手與helper.name_of_the_helper
。
這個特殊的例子包括ActionView's NumberHelper。我需要UrlHelper,所以我做了include ActionView::Helpers::UrlHelper
和helper.link_to
。
- 1. RSpec:如何測試從控制器調用私人幫助器方法的幫助器方法?
- 2. 測試幫助器方法w/RSpec
- 3. 有沒有人用RSpec測試過best_in_place測試幫助器方法?
- 4. 如何測試對象上是否調用方法 - Rails RSpec
- 5. rspec測試如果方法被調用
- 6. 需要幫助Rspec測試
- 7. Rspec幫助程序測試需要訪問控制器方法
- 8. 從Rails 3控制器調用幫助器方法
- 9. Rspec測試不應該使用自己的幫助器方法失敗,爲什麼呢? Rails
- 10. rspec-sinatra,如何調用輔助方法?
- 11. rails/rspec中請求規範的存根幫助器方法
- 12. 用rspec測試rails引擎助手
- 13. 如何在Ruby on Rails中使用Rspec測試鏈式方法
- 14. Javascript調用Rails幫助器
- 15. Rails自動調用應用程序幫助器方法隨時調用form_for
- 16. 的Rails如何測試方法接收使用RSpec
- 17. Rails 5 - 幫助器方法
- 18. 如何使用JavaScript幫助調用Rails方法
- 19. 調試rails應用程序,調試器關鍵字是唯一的方法嗎?任何IDE幫助?
- 20. 從Grails中的過濾器幫助器方法調用render()
- 21. 單元測試Rails應用程序的控制器的幫助模塊 - 如何?
- 22. 如何從自定義幫助器方法調用Ajax.BeginForm?
- 23. Rspec的/ Rails的測試方法被調用的對象上的控制器內
- 24. 如何測試在一個構造方法調用使用RSpec
- 25. 使用RSpec測試Rails控制器 - 如何測試:current_account.projects
- 26. 如何測試rspec中的after_create方法?
- 27. 如何測試RSPEC中的類方法
- 28. RubyOnRails:如何在Rails控制檯中使用幫助器方法?
- 29. 如何調用幫助方法
- 30. Rspec的測試Rails應用程序
你能告訴如何調用該方法嗎? 我已經嘗試過'SearchHelper :: formatResultDate(r.date)'和'formatResultDate(r.date)',但又得到另一個「未定義的方法」。 Thks –
是你自己的幫手嗎? SearchHelper是一個模塊嗎?如果是這樣,只需用'include SearchHelper'替換'inlucde ActionView :: Helpers',然後你就可以用'formatResultDate(r.date)'調用該方法。 –
測試郵件程序時,這並不適用於我。例如'expect(mail.body).to match(I18n.t('some.path',argument:link_to(value,custom_url_helper(value)))' –