How to complete the rspec put controller test from scaffold

前端 未结 6 1338
Happy的楠姐
Happy的楠姐 2020-12-24 11:58

I\'m using scaffolding to generate rspec controller tests. By default, it creates the test as:

  let(:valid_attributes) {
    skip(\"Add a hash of attributes         


        
6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-24 12:45

    Testing the rails application with rspec-rails gem. Created the scaffold of user. Now you need to pass all the examples for the user_controller_spec.rb

    This has already written by the scaffold generator. Just implement

    let(:valid_attributes){ hash_of_your_attributes} .. like below
    let(:valid_attributes) {{ first_name: "Virender", last_name: "Sehwag", gender: "Male"}
      } 
    

    Now will pass many examples from this file.

    For invalid_attributes be sure to add the validations on any of field and

    let(:invalid_attributes) {{first_name: "br"}
      }
    

    In the users model .. validation for first_name is as =>

      validates :first_name, length: {minimum: 5}, allow_blank: true
    

    Now all the examples created by the generators will pass for this controller_spec

提交回复
热议问题