1

When running my specs I get the following error:

Failures: 1) UserMailer account_activation renders the headers Failure/Error: mail.subject.should eq("Bazleyapp account activation") ArgumentError: wrong number of arguments (0 for 1)

I can't solve the wrong number of arguments problem. What is it referring to? The error message isn't very helpful.

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

config/environments/test.rb:

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

EDIT:

I made the change that BroiSatse suggested and I now get this error:

UserMailer account_activation renders the headers Failure/Error: let(:user) { FactoryGirl.create(:user) } ArgumentError: wrong number of arguments (1 for 0)

In fact I have just noticed that this error is appearing throughout my whole test suite wherever

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

appears. So the problem has shifted to FactoryGirl?

My factories.rb file is:

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
Bazley
  • 2,699
  • 5
  • 36
  • 61
  • PLease post your user factory together with user model. – BroiSatse Dec 09 '14 at 17:15
  • Hmmmm, don't see anything obvious. Have you tried opening a console and check: `FactoryGirl.create :user`? – BroiSatse Dec 09 '14 at 17:34
  • Tried it: got the response "NameError: uninitialized constant FactoryGirl" – Bazley Dec 09 '14 at 17:40
  • But I think that's because `gem 'factory_girl_rails', '4.2.0'` is in the `group :test` in my gemfile. When I moved it to the top of the gemfile I got this: (0.2ms) BEGIN User Exists (1.0ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('user_1@example.com') LIMIT 1 User Exists (0.5ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."callsign") = LOWER('user_1') LIMIT 1 (0.2ms) ROLLBACK ArgumentError: wrong number of arguments (1 for 0) – Bazley Dec 09 '14 at 19:47
  • Any backtrace? Try creating new user the usual way. `User.create [attributes]` – BroiSatse Dec 10 '14 at 09:30
  • User.create(...) works fine in the console. I've added the full backtrace to the question as an edit. – Bazley Dec 10 '14 at 13:56

1 Answers1

1

This is due to an incompatibility between ActiveRecord 4.2 Beta 4 and the newly released Arel and is addressed in https://stackoverflow.com/a/27140014/1008891

When I went through Michael Hartl's tutorial he was pretty adamant that his tutorial is only guaranteed to work with the specific versions of the gems he identified.

Community
  • 1
  • 1
Peter Alfvin
  • 28,599
  • 8
  • 68
  • 106
  • How would you fix this? – Mary Jun 23 '15 at 00:36
  • Check the link. At the risk of repeating myself, your only guarantee is to stick to the versions prescribed in the tutorial. Otherwise, you can upgrade Rails and/or Arel and see if you can find two that are compatible. – Peter Alfvin Jun 23 '15 at 01:23