0

我正在尝试使用 RSpec 为我的 Rails 应用程序编写一些单元测试。我正在使用 Rails4.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中(白|黑)列出参数的新方法。

4

0 回答 0