依赖:: dev不工作,rails 3.1

时间:2012-01-30 00:53:39

标签: ruby-on-rails-3 model

我有一个rails 3.1 app,我有这个简单的用户规范

require 'spec_helper'

describe User do
  describe "deleting user" do
    let(:user) { FactoryGirl.create(:user) }
    context "that has accounts" do
      it "deletes the user an all its accounts" do
        FactoryGirl.create(:account, name: 'Santander', user: user)
        FactoryGirl.create(:account, name: 'BCI', user: user)
        user.accounts.length.should == 2
        user.delete
        user_accounts = Account.where(user_id: user.id)
        user_accounts.should be_nil
      end
    end
  end
end

并且模型是

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :email, :password, :password_confirmation, :remember_me

  has_many :accounts, dependent: :destroy
  has_many :categories, dependent: :destroy
end

class Account < ActiveRecord::Base
  belongs_to :user
end

我收到以下错误

1) User deleting user that has accounts deletes the user an all its accounts
     Failure/Error: user_accounts.should be_nil
       expected: nil
            got: [#<Account id: 400, name: "BCI", start_balance: 0, atype: "checking_account", user_id: 1692, current_balance: 0, interest_rate: nil, billing_day: nil, created_at: "2012-01-30 00:42:51", updated_at: "2012-01-30 00:42:51">, #<Account id: 399, name: "Santander", start_balance: 0, atype: "checking_account", user_id: 1692, current_balance: 0, interest_rate: nil, billing_day: nil, created_at: "2012-01-30 00:42:51", updated_at: "2012-01-30 00:42:51">]
     # ./spec/models/user_spec.rb:13:in `block (4 levels) in <top (required)>'

它不会发生......问题是什么?我在开发环境中的rails控制台中手动尝试了它并且它也没有工作

1 个答案:

答案 0 :(得分:0)

我不确定但是试试这个:

describe User do
  describe "deleting user" do
    let(:user) { FactoryGirl.create(:user) }
    context "that has accounts" do
      it "deletes the user an all its accounts" do
        FactoryGirl.create(:account, name: 'Santander', user: user)
        FactoryGirl.create(:account, name: 'BCI', user: user)
        user.accounts.length.should == 2
        user.delete :destroy
        user_accounts = Account.where(user_id: user.id)
        user_accounts.should be_nil
      end
    end
  end
end
编辑:我已经修改了一点,特别是关于delete行。拍了一下。