Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9070613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:43:33+00:00 2026-06-16T17:43:33+00:00

I’m attempting to test the creation of an Reservation model that involves processing a

  • 0

I’m attempting to test the creation of an Reservation model that involves processing a payment with ActiveMerchant upon creation.

The initial setup for payment processing involved following the ActiveMerchant Railscasts. Payments are working fine within the app. (http://railscasts.com/episodes/145-integrating-active-merchant)

I’ve tried creating the credit_card object within the Reservation factory and within it’s own “:valid_credit_card” factory…

The basic test is just attempting to verify the Reservation can be created.
Test results:

1) Reservation should have a valid factory
 Failure/Error: @current_reservation = Factory.create(:reservation)
 NoMethodError:
 undefined method `credit_card=' for #<Reservation:0xb5f6173c>
 # ./spec/models/reservation_spec.rb:11

The Reservation belongs_to a user and has_many rooms through reservation_sets

Factory.define :reservation do |f|
  f.association :user
  f.rooms { |a| [a.association(:room)] }
  f.arrival Time.now + 2.weeks
  f.nights  2
  f.phone "555-123-1234"
  f.credit_card :valid_credit_card
end

Factory.define :valid_credit_card, :class => ActiveMerchant::Billing::CreditCard do |f|
  expiration_date = Time.zone.now + 1.year  
  f.type "visa"
  f.number "4111111111111111"
  f.verification_value "333"
  f.month expiration_date.strftime("%m")
  f.year expiration_date.strftime("%y")
  f.first_name "Bob"
  f.last_name "Smith"
end

And the spec/models/reservation_spec.rb. Using @credit_card Factory.build causes errors about “saving” the credit_card.

If I remove the line f.credit_card :valid_credit_card I get the NoMethodError for :month
even though :month is listed in attr_accessor. The creation of a reservation within the app does work.

  1) Reservation should have a valid factory
     Failure/Error: @current_reservation = Factory.create(:reservation)
     NoMethodError:
       undefined method `month' for nil:NilClass

describe Reservation do
  before :each do
    @smith = Factory.create(:user)
    @room = Factory.create(:room)
    #@credit_card = Factory.build(:valid_credit_card) 
  end
  it "should have a valid factory" do
    @current_reservation = Factory.create(:reservation)
    @current_reservation.should be_valid
  end
end

What am I overlooking / doing incorrectly…?

Reservation model excerpts

class Reservation < ActiveRecord::Base
  # relationships
  belongs_to :user
  has_many :reservation_sets, 
       :dependent => :destroy
  has_many :rooms, 
           :through => :reservation_sets
  has_many :transactions,
           :class_name => 'ReservationTransaction',
           :dependent => :destroy

  attr_accessor :card_number, :card_verification, :card_expires_on, :card_type, :ip_address, :rtype, :month, :year
  # other standard validations
  validate :validate_card, :on => :create

  # other reservation methods...
  # gets paid upon reservation creation
  def pay_deposit
    # Generate active merchant object

    ReservationTransaction.gateway = 
      ActiveMerchant::Billing::AuthorizeNetGateway.new({
        :login => rooms[0].user.gateway_login,
        :password => rooms[0].user.gateway_password
      }) 

    response = ReservationTransaction.gateway.purchase(deposit_price_in_cents, credit_card, purchase_options)
    t = transactions.create!(:action => "purchase", :amount => deposit_price_in_cents, :response => response)
    if response.success?
      update_attribute(:reserved_at, Time.now)
      # update state
      payment_captured!
    else
      transaction_declined!
      errors.add :base, response.message
    end
    t.card_number = credit_card.display_number
    t.save!
    response.success?
  end

  def validate_card
    unless credit_card.valid?
      credit_card.errors.full_messages.each do |message|
        errors.add :base, message #_to_base message
      end
    end
  end

  def credit_card
    @credit_card ||= ActiveMerchant::Billing::CreditCard.new(
      :type               => card_type,
      :number             => card_number,
      :verification_value => card_verification,
      :month              => card_expires_on.month,
      :year               => card_expires_on.year,
      :first_name         => first_name,
      :last_name          => last_name
    )
  end

And the create action from the Reservation Controller

  def create
    @reservation = Reservation.new(params[:reservation])
    @reservation.arrival = session[:arrival]
    @reservation.nights = session[:nights]
    @reservation.number_kids = session[:number_kids]
    @reservation.number_adults = session[:number_adults]
    session[:creating_reservation] = 1
    @reservation.user_id = @reservation.rooms[0].user_id
    session[:owner] = @reservation.user_id
    @rooms = Room.all
    @reservation.ip_address = request.remote_ip         

    # get room owner...
    @owner = User.find(@reservation.user_id) 
    respond_to do |format|
      if @reservation.save
        if @reservation.pay_deposit
          #set cc...
          @reservation.transactions[0].card_number = @reservation.send(:credit_card).display_number
          ReservationMailer.reservation_created(@reservation).deliver
          ReservationMailer.reservation_notice(@reservation).deliver
          session[:arrival] = nil
          session[:reservation_id] = @reservation.id
          if @owner 
            thanks_path = "#{@owner.permalink}/reservations/#{@reservation.id}" 
          else
            thanks_path = @reservation
          end
          format.html { redirect_to @reservation, :notice => 'Reservation was successfully created.' }
          format.json { render :json => @reservation, :status => :created, :location => @reservation }
          # also trigger email sending or wherever that is 
          # receipt email and order notification
          # 
        else
           # set flash or show message problem w/ transaction 

           format.html { render :action => "new" }
        end
      else
        format.html { render :action => "new" }
        format.json { render :json => @reservation.errors, :status => :unprocessable_entity }
      end
    end
  end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:43:34+00:00Added an answer on June 16, 2026 at 5:43 pm

    It looks like you are trying to assign credit_card a value, but you don’t really have a class accessor. So where you are trying to call f.credit_card :valid_credit_card isn’t going to work.

    I would remove the f.credit_card :valid_credit_card from your factory and look into using rspec stubs, then you could do something like the following in your rspec test:

    mock_cc = ActiveMerchant::Billing::CreditCard.new(
          :type               => card_type,
          :number             => card_number,
          :verification_value => card_verification,
          :month              => card_expires_on.month,
          :year               => card_expires_on.year,
          :first_name         => first_name,
          :last_name          => last_name
        )
    
    Reservation.stub(:credit_card).and_return(mock_cc)
    

    This would make it so when your model called credit_card it would return a mocked object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.