2

Rspec-rails tests started to fail after adding Aasm gem. All basic set ups are done. Migration with adding aasm_state column. Adding state to the Order model.

Test log:

Searching book Visitor searches books by title
 Failure/Error: order = Order.create

 ActionView::Template::Error:
   undefined method `aasm_state' for #<Order:0x0000000008e48740>
   Did you mean?  aasm_state=

However there are no Order in the test:

require 'spec_helper'
require 'rails_helper'

feature 'Searching book' do

  before do
    book = Book.create title: "Geralt"
    author = Author.create full_name: "Swiss"
    category = Category.create title: "Programming"
    category.books << book
    book.authors << author
    author.books << book
    visit books_path
  end

  scenario 'Visitor searches books by title' do
    fill_in 'search', with: "Geralt"
    click_button('Search')
    expect(page).to have_content 'Geralt'
  end

end

Maybe issue is in application controller helper method:

class ApplicationController < ActionController::Base
  helper_method :current_order
  protect_from_forgery with: :exception

  def current_order
    if session[:order_id].nil?
      order = Order.create
      session[:order_id] = order.id
      order
    else
      Order.find(session[:order_id])
    end
  end

end

Order.rb with aasm, part:

  include AASM
  aasm do
    state :in_progress, initial: true
    state :processing
    state :in_delivery
    state :delivered
    state :canceled
  end

Template:

= form_tag books_path, method: :get do
  .form-group.col-sm-2
    = text_field_tag :search, params[:search], class: "form-control", id: "search",placeholder: "Search book"
  = submit_tag "Search", class: "btn btn-primary"

= render @books

= "Items in cart #{current_order.order_items.count}"
StanisLove Sid
  • 322
  • 3
  • 9
  • The Actionview::template::Error tells you the error is coming from your view. Show the view that is being rendered – Thomas Walpole Nov 09 '18 at 02:53
  • @ThomasWalpole thanks for your response. Added the view – StanisLove Sid Nov 09 '18 at 10:03
  • Ok -- still not showing where exactly you're using `aasm_state` - It appears to be either in the template that `render @books` uses or while trying to get the order_items.count -- remove each from the page and see which one gets rid of the error - then look into that. – Thomas Walpole Nov 09 '18 at 17:20

0 Answers0