1

我刚开始在一个新项目中使用 RSpec(在成为 minitest 用户一段时间后)。我创建了一个名为contracts 的MVC。这是模型文件:

class Contract < ActiveRecord::Base
  attr_accessible :name, :number, :plannedStart, :actualStart, :plannedCompletion, :actualCompletion
end

我已经为合同定义了一个基本工厂(之前它稍微复杂一些,使用序列来生成新的名称和数字,但我删除了所有这些以试图简化这个错误的压缩):

FactoryGirl.define do

  factory :contract do

  end
end

以下是规格(几乎是自动生成的,除了我在对工厂的调用中添加了):

require File.dirname(__FILE__) + '/../spec_helper'

describe ContractsController do
  render_views

  it "index action should render index template" do
    create(:contract)
    get :index
    response.should render_template(:index)
  end

  it "show action should render show template" do

    create(:contract)
    get :show, :id => Contract.first
    response.should render_template(:show)
  end

  it "new action should render new template" do
    get :new
    response.should render_template(:new)
  end

  it "create action should render new template when model is invalid" do

    Contract.any_instance.stubs(:valid?).returns(false)
    post :create
    response.should render_template(:new)
  end

  it "create action should redirect when model is valid" do

    Contract.any_instance.stubs(:valid?).returns(true)
    post :create
    response.should redirect_to(contract_url(assigns[:contract]))
  end

  it "edit action should render edit template" do
    create(:contract)
    get :edit, :id => Contract.first
    response.should render_template(:edit)
  end

  it "update action should render edit template when model is invalid" do
    create(:contract)
    Contract.any_instance.stubs(:valid?).returns(false)
    put :update, :id => Contract.first
    response.should render_template(:edit)
  end

  it "update action should redirect when model is valid" do
    create(:contract)
    create(:contract)
    Contract.any_instance.stubs(:valid?).returns(true)
    put :update, :id => Contract.first
    response.should redirect_to(contract_url(assigns[:contract]))
  end

  it "destroy action should destroy model and redirect to index action" do
    create(:contract)
    contract = Contract.first
    delete :destroy, :id => contract
    response.should redirect_to(contracts_url)
    Contract.exists?(contract.id).should be_false
  end
end

当我运行规范时,我收到以下错误消息,但我得到的这些错误消息的数量因运行而异:

ActiveRecord::RecordInvalid: Validation failed: 
./spec/controllers/contracts_controller_spec.rb:39:in `block (2 levels) in <top (required)>'

ActiveRecord::RecordInvalid: Validation failed: 
./spec/controllers/contracts_controller_spec.rb:45:in `block (2 levels) in <top (required)>'

ActiveRecord::RecordInvalid: Validation failed: 
./spec/controllers/contracts_controller_spec.rb:7:in `block (2 levels) in <top (required)>'

10 examples, 3 failures, 7 passed

Finished in 0.360139 seconds
/Users/jlee/.rvm/rubies/ruby-1.9.3-p194/bin/ruby -S rspec ./spec/controllers/contracts_controller_spec.rb ./spec/models/contract_spec.rb failed

我已经实现了 Databasecleaner 只是为了确保这不是与在测试期间使用事务有关的一些奇怪的行为问题,但无济于事。建议?我没有定义验证,所以很难理解我怎么会失败验证。

4

1 回答 1

0

:valid? 的存根 在测试之间徘徊,所以我将以下内容放在 before(:each) 块中:Contract.any_instance.unstub(:valid?),它清除了所有内容。

于 2013-09-24T20:25:39.097 回答