脚手架生成的代码没有通过rspec

时间:2014-09-21 17:52:48

标签: ruby-on-rails ruby shell rspec

我是Rspec的新手,我正在学习一个教程,我在一个新的rails项目上运行了以下命令:

bundle exec rails generate scaffold Person first_name:string last_name:string
bundle exec rake db:migrate db:test:prepare
bundle exec rspec

我得到15次失败,其中一些失败如下:

1) PeopleController POST create with valid params redirects to the created person
 Failure/Error: response.should redirect_to(Person.last)
 NoMethodError:
   undefined method `assertions' for #<RSpec::Rails::TestUnitAssertionAdapter::AssertionDelegator:0x007fe7b2417980>
 # ./spec/controllers/people_controller_spec.rb:80:in `block (4 levels) in <top (required)>'

 2) PeopleController POST create with invalid params assigns a newly created but unsaved person as @person
 Failure/Error: post :create, {:person => { "first_name" => "invalid value" }}, valid_session
 ArgumentError:
   wrong number of arguments (1 for 2+)
 # ./app/controllers/people_controller.rb:30:in `block in create'
 # ./app/controllers/people_controller.rb:29:in `create'
 # ./spec/controllers/people_controller_spec.rb:88:in `block (4 levels) in <top (required)>'

 3) PeopleController POST create with invalid params re-renders the 'new' template
 Failure/Error: post :create, {:person => { "first_name" => "invalid value" }}, valid_session
 ArgumentError:
   wrong number of arguments (1 for 2+)
 # ./app/controllers/people_controller.rb:30:in `block in create'
 # ./app/controllers/people_controller.rb:29:in `create'
 # ./spec/controllers/people_controller_spec.rb:95:in `block (4 levels) in <top (required)>'

 4) PeopleController DELETE destroy redirects to the people list
 Failure/Error: response.should redirect_to(people_url)
 NoMethodError:
   undefined method `assertions' for #<RSpec::Rails::TestUnitAssertionAdapter::AssertionDelegator:0x007fe7b41b9510>
 # ./spec/controllers/people_controller_spec.rb:156:in `block (3 levels) in <top (required)>'

5) PeopleController PUT update with valid params redirects to the person
 Failure/Error: response.should redirect_to(person)
 NoMethodError:
   undefined method `assertions' for #<RSpec::Rails::TestUnitAssertionAdapter::AssertionDelegator:0x007fe7b3a4c188>
 # ./spec/controllers/people_controller_spec.rb:122:in `block (4 levels) in <top (required)>'
 ...........

这是人民控制人员所指的

class PeopleController < ApplicationController
before_action :set_person, only: [:show, :edit, :update, :destroy]

# GET /people
# GET /people.json
def index
  @people = Person.all
end

# GET /people/1
# GET /people/1.json
def show
end

# GET /people/new
def new
   @person = Person.new
end

# GET /people/1/edit
def edit
end

# POST /people
# POST /people.json
def create
@person = Person.new(person_params)

respond_to do |format|
  if @person.save
    format.html { redirect_to @person, notice: 'Person was successfully created.' }
    format.json { render :show, status: :created, location: @person }
  else
    format.html { render :new }
    format.json { render json: @person.errors, status: :unprocessable_entity }
  end
end
end

# PATCH/PUT /people/1
# PATCH/PUT /people/1.json
def update
respond_to do |format|
  if @person.update(person_params)
    format.html { redirect_to @person, notice: 'Person was successfully updated.' }
    format.json { render :show, status: :ok, location: @person }
  else
    format.html { render :edit }
    format.json { render json: @person.errors, status: :unprocessable_entity }
  end
  end
end

# DELETE /people/1
# DELETE /people/1.json
def destroy
@person.destroy
respond_to do |format|
  format.html { redirect_to people_url, notice: 'Person was successfully destroyed.' }
  format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_person
  @person = Person.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def person_params
  params.require(:person).permit(:first_name, :last_name)
end
end

为什么脚手架生成的代码未通过这些rspec测试?

1 个答案:

答案 0 :(得分:1)

你还在使用minitest吗?尝试将gemfile中的版本更改为例如:

gem 'minitest', '~> 4.0'