RSPEC Controller Test Reconfiguration

Source: Internet
Author: User

(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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.