2014-09-21 54 views
1

我新的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测试失败?

+0

看起来这可能是[这个问题]一个DUP(http://stackoverflow.com/questions/16867707/rails-4-and-rspec-undefined -method-assertions-in-routing-spec) – Ben 2014-09-21 18:12:56

+0

@Ben,该问题的解决方案奏效!我只跑了:gem“rspec-rails”,'〜> 2.14.0.rc1'。谢谢! – 2014-09-21 18:40:28

回答

1

你也使用minitest?试着改变你的Gemfile的版本如:

gem 'minitest', '~> 4.0' 
+0

我改变了MINITEST到4.0,得到了一个错误:捆扎机找不到兼容版本的宝石 “MINITEST”: 在Gemfile中: MINITEST(〜> 4.0)红宝石 的JBuilder(〜> 2.0)红宝石取决于 的ActiveSupport( < 5, > = 3.0.0)ruby取决于 minitest(5.1.0) – 2014-09-21 18:22:49

+0

Ty锁定版本为gem'minitest','〜> 5.1.0' – userden 2014-09-21 18:26:25