Rails + RSpec中缺少属性

时间:2013-10-31 20:51:31

标签: ruby-on-rails-4 rspec2 strong-parameters

我正在尝试使用RSpec为我的Rails应用程序编写一些单元测试。我正在使用Rails 4.0.0和Rspec-Rails 2.14.6。我的User型号:

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
    :recoverable, :rememberable, :trackable, :validatable,
    :omniauthable, :omniauth_providers => Settings.authentication.providers.map { |i| i.to_sym }

  validates :password, presence: true, allow_nil: true,
    allow_blank: true, if: :password_required?
  validates :email, presence: true

  def password_required?
    self.providers.empty?
  end

  has_many :providers
  has_one :profile
end

我的(神奇)规范:

require 'spec_helper'

describe User do
  describe '.new' do
    it 'can create a valid user with no providers'  do
      params = ActionController::Parameters.new(FactoryGirl.attributes_for :user)
      u = User.create params.permit!
      expect(u).to be_new_record
    end
  end

  describe '.build_with_provider' do
    it 'can create a valid user with a provider' do
      puts ap(User)
      provider   = FactoryGirl.create :provider
      oauth_data = FactoryGirl.attributes_for :oauth_data
      u = User.build_from_provider_and_oauth_data provider, oauth_data
      expect(u).to_not be_nil
      expect(u).to be_new_record
      expect(u).to be_valid
      u.save
      expect(u).to be_persisted
    end
  end
end

如果我遗失了什么,请告诉我。据我所知,使用ActionController::Parameters是Rails 4中列出参数(白色|黑色)的新方法。

0 个答案:

没有答案