我新的Rspec的和我下面的教程,我跑了一個新的Rails項目如下命令:腳手架生成的代碼不通過rspec的
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
爲什麼支架產生的CO de這些rspec測試失敗?
看起來這可能是[這個問題]一個DUP(http://stackoverflow.com/questions/16867707/rails-4-and-rspec-undefined -method-assertions-in-routing-spec) – Ben 2014-09-21 18:12:56
@Ben,該問題的解決方案奏效!我只跑了:gem「rspec-rails」,'〜> 2.14.0.rc1'。謝謝! – 2014-09-21 18:40:28