Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
889 views
in Technique[技术] by (71.8m points)

ruby on rails - Where should I store methods to make them available both to my application and to RSpec?

I need to create some methods that are available both to my application (models, views and controllers) and to RSpec.

Specifically I need to create some path_helper methods that contain critical logic. I need to make them available to RSpec so that I can run tests like:

controller.should redirect_to my_custom_path_helper(@object)

I presume that they should go somewhere in /lib however I'm not sure how to structure this (mixin module - if so what should I mix the module into?).

An alternative option would be if I could access Application Helpers from RSpec.

EDIT: Please note

This is not a question about testing helper methods it is about making them accessible to both the application and RSpec.

I need to do this because I need wrapper logic around my rails path_helpers and when I test should redirect_to .._path in RSpec I don't want to duplicate the logic of those path helpers.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can make RSpec include a module by (in spec/spec_helper.rb):

RSpec.configure do |config|
  config.include YourModule
end

If you want to use routing path helpers, you can include them like this:

RSpec.configure do |config|
  config.include Rails.application.routes.url_helpers
end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...