rspec测试错误的参数数量

时间:2014-12-09 16:15:12

标签: ruby-on-rails rspec

运行我的规格时出现以下错误:

失败:   1)UserMailer account_activation呈现标题      失败/错误:mail.subject.should eq(" Bazleyapp帐户激活")      引发ArgumentError:        错误的参数数量(0表示1)

我无法解决错误数量的参数问题。它指的是什么?错误消息非常有用。

user_mailer_spec.rb:

require "spec_helper"

describe UserMailer do
  describe "account_activation" do
    let(:mail) { UserMailer.account_activation }
    it "renders the headers" do
      mail.subject.should eq("Bazleyapp account activation")
    end
  end
end

user_mailer.rb:

class UserMailer < ActionMailer::Base
  default from: "noreply@bazleyapp.com"
  def account_activation(user)
    @user = user
    mail to: user.email, subject: "Bazleyapp account activation"
  end
end

配置/环境/ test.rb:

config.action_mailer.delivery_method = :test
config.action_mailer.default_url_options = { host: 'localhost:3000' }

编辑:

我做了BroiSatse建议的更改,现在我收到了这个错误:

UserMailer account_activation呈现标题失败/错误:let(:user){FactoryGirl.create(:user)} ArgumentError:错误的参数数量(1表示0)

事实上,我刚刚发现这个错误出现在整个测试套件中的任何地方

let(:user) { FactoryGirl.create(:user) }

出现。那问题已转移到FactoryGirl?

我的factories.rb文件是:

FactoryGirl.define do

  factory :user do
    sequence(:name)     { |n| "User #{n}" }
    sequence(:email)    { |n| "user_#{n}@example.com" }
    sequence(:callsign) { |n| "user_#{n}" }
    password "foobar"
    password_confirmation "foobar"
    activated true

    factory :admin do
      admin true
    end
  end
end

User.rb:

class User < ActiveRecord::Base
  attr_accessor :remember_token, :activation_token, :reset_token
  has_one  :ghost,    dependent: :destroy
  has_many :personas, dependent: :destroy
  has_secure_password

  before_save do
    self.email.downcase!
    self.callsign.downcase!
  end
  before_create :create_activation_digest
  after_save do
    self.create_ghost unless ghost
  end

  validates :name, presence: true,
                   length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(?:\.[a-z\d\-]+)*\.[a-z]+\z/i
  validates :email, presence:   true,
                    format:     { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  VALID_CALLSIGN_REGEX = /\A[a-z\d\-.\_]+\z/i
  validates :callsign, presence:   true,
                       length:     { maximum: 20 },
                       format:     { with: VALID_CALLSIGN_REGEX },
                       uniqueness: { case_sensitive: false }
  validates :password, length: { minimum: 6 }

  def to_param
    callsign
  end

  def self.digest(string)
    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
                                                  BCrypt::Engine.cost
    BCrypt::Password.create(string, cost: cost)
  end

  def self.new_token
    SecureRandom.urlsafe_base64
  end

  def remember
    self.remember_token = User.new_token
    update_attribute(:remember_digest, User.digest(remember_token))
  end

  def forget
    update_attribute(:remember_digest, nil)
  end

  def authenticated?(attribute, token)
    digest = send("#{attribute}_digest")
    return false if digest.nil?
    BCrypt::Password.new(digest).is_password?(token)
  end

  def activate
    update_columns(activated: true, activated_at: Time.zone.now)
  end

  def send_activation_email
    UserMailer.account_activation(self).deliver_now
  end

  def create_reset_digest
    self.reset_token = User.new_token
    update_columns(reset_digest:  User.digest(reset_token),
                   reset_sent_at: Time.zone.now)
  end

  def send_password_reset_email
    UserMailer.password_reset(self).deliver_now
  end

  def password_reset_expired?
    reset_sent_at < 2.hours.ago
  end

  private

    def create_activation_digest
      self.activation_token  = User.new_token
      self.activation_digest = User.digest(activation_token)
    end

end

1 个答案:

答案 0 :(得分:1)

这是由于ActiveRecord 4.2 Beta 4与新发布的Arel之间不兼容,并在https://stackoverflow.com/a/27140014/1008891中解决了

当我通过Michael Hartl的教程时,他非常坚定地认为他的教程只能保证与他所识别的宝石的特定版本一起使用。