(1) When it is necessary to re-use the test situation, you can use the Shared_examples ("describe") do end, where needed to use the It_behaves_like "describe" reuse, code written in Controller_ In the Spec.rb file
(2) A large number of reusable code can be put forward separately, placed in the spec/support/in the module, and in the Spec/spec_helper.rb file in the Rspec.configure block, add config.include Moudle, or directly under the Spec/support or sub-folder to write a RB file, SPEC_HELPER.RB will automatically require, the following code:
Dir[rails.root.join ("spec/support/**/*.rb")].each {|f| require F}
(3) You can use a custom match to simplify the code, such as the login function:
Spec/support/matchers/require_login.rb
rspec::matchers.define :require_login do |attribute| match do |actual| expect (attribute) .to redirect_to rails.application.routes.url_helpers.login_ path end failure_message_for_should do |actual| "expected to require login to access The method " end failure_message_for _should_not do |actual| "expected not to require Login ro access the method " end description do "Redirect to the login form" endEnd
Use:
Expect (response) to Require_login
RSPEC Controller Test Reconfiguration