small medium large xlarge

Generic-user-small
24 Sep 2017, 16:39
mike hsieh (1 post)

I am getting the following error when I ran ‘bundle exec rspec spec//integration/app/ledger_spec.rb’.

What is missing here?

FF

Failures:

1) ExpenseTracker::Ledger#record with a valid expense successfully saves the expense in the DB Failure/Error: DB[:expenses].insert(expense)

 Sequel::DatabaseError:
   SQLite3::SQLException: no such table: expenses
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:91:in `initialize'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:91:in `new'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:91:in `prepare'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:137:in `execute'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sequel-4.48.0/lib/sequel/adapters/sqlite.rb:197:in `block (2 levels) in _execute'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sequel-4.48.0/lib/sequel/database/logging.rb:45:in `log_connection_yield'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sequel-4.48.0/lib/sequel/adapters/sqlite.rb:197:in `block in _execute'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sequel-4.48.0/lib/sequel/database/connecting.rb:301:in `block in synchronize'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sequel-4.48.0/lib/sequel/connection_pool/threaded.rb:107:in `hold'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sequel-4.48.0/lib/sequel/database/connecting.rb:301:in `synchronize'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sequel-4.48.0/lib/sequel/adapters/sqlite.rb:188:in `_execute'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sequel-4.48.0/lib/sequel/adapters/sqlite.rb:154:in `execute_insert'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sequel-4.48.0/lib/sequel/dataset/actions.rb:1097:in `execute_insert'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sequel-4.48.0/lib/sequel/dataset/actions.rb:402:in `insert'
 # ./app/ledger.rb:11:in `record'
 # ./spec/integration/app/ledger_spec.rb:20:in `block (4 levels) in <module:ExpenseTracker>'
 # ------------------
 # --- Caused by: ---
 # SQLite3::SQLException:
 #   no such table: expenses
 #   /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:91:in `initialize'

2) ExpenseTracker::Ledger#record when the expense lacks a payee rejects the expense as invalid Failure/Error: expect(DB[:expenses].count).to eq(0)

 Sequel::DatabaseError:
   SQLite3::SQLException: no such table: expenses
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:91:in `initialize'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:91:in `new'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:91:in `prepare'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:264:in `query'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sequel-4.48.0/lib/sequel/adapters/sqlite.rb:195:in `block (2 levels) in _execute'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sequel-4.48.0/lib/sequel/database/logging.rb:45:in `log_connection_yield'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sequel-4.48.0/lib/sequel/adapters/sqlite.rb:195:in `block in _execute'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sequel-4.48.0/lib/sequel/database/connecting.rb:301:in `block in synchronize'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sequel-4.48.0/lib/sequel/connection_pool/threaded.rb:107:in `hold'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sequel-4.48.0/lib/sequel/database/connecting.rb:301:in `synchronize'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sequel-4.48.0/lib/sequel/adapters/sqlite.rb:188:in `_execute'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sequel-4.48.0/lib/sequel/adapters/sqlite.rb:133:in `execute'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sequel-4.48.0/lib/sequel/dataset/actions.rb:1081:in `execute'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sequel-4.48.0/lib/sequel/adapters/sqlite.rb:337:in `fetch_rows'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sequel-4.48.0/lib/sequel/dataset/actions.rb:942:in `with_sql_each'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sequel-4.48.0/lib/sequel/dataset/actions.rb:950:in `with_sql_first'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sequel-4.48.0/lib/sequel/dataset/actions.rb:958:in `with_sql_single_value'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sequel-4.48.0/lib/sequel/dataset/actions.rb:773:in `single_value!'
 # /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sequel-4.48.0/lib/sequel/dataset/actions.rb:110:in `count'
 # ./spec/integration/app/ledger_spec.rb:42:in `block (4 levels) in <module:ExpenseTracker>'
 # ------------------
 # --- Caused by: ---
 # SQLite3::SQLException:
 #   no such table: expenses
 #   /Users/chaohohsieh/.rvm/gems/ruby-2.4.1@global/gems/sqlite3-1.3.13/lib/sqlite3/database.rb:91:in `initialize'

Finished in 0.01829 seconds (files took 0.25825 seconds to load) 2 examples, 2 failures

Failed examples:

rspec ./spec/integration/app/ledger_spec.rb:19 # ExpenseTracker::Ledger#record with a valid expense successfully saves the expense in the DB rspec ./spec/integration/app/ledger_spec.rb:33 # ExpenseTracker::Ledger#record when the expense lacks a payee rejects the expense as invalid

Myron-profile-img_pragsmall
25 Sep 2017, 04:55
Myron Marston (13 posts)

Hi Mike,

The exception is telling you that the expenses table does not exist. There are a couple of things that could cause this:

  • Your migration could be creating a different table (e.g. by mistyping the table name in the migration as expnses or something). Check db/migrations/0001_create_expenses.rb to make sure it’s been defined properly according to the code listing on page 84.
  • The migration isn’t running as part of your spec suite, which would naturally lead to the expenses table not being created in your test database. In the book, we hook things up in the test environment so that the migration runs in a before(:suite) hook via a Sequel::Migrator.run(DB, 'db/migrations') line of code. See the listing for spec/support/db.rb on page 85 for the bit of code I’m talking about. Maybe you haven’t defined this hook properly, or maybe it’s not being loaded. Make sure you’re requiring this file – it’s not loaded automatically by RSpec. In the book, we initially load it via a require_relative '../../support/db' line (in spec/integration/app/ledger_spec.rb–see page 86), and later move the require to a config.when_first_matching_example_defined hook in spec_helper.rb (see page 93).

If your problem isn’t obvious by looking over these bits of code, I recommend trying to add some puts statements in the before(:suite) hook and in the migration file itself to see if they are running when you run rspec. You can also compare your code to ours by cloning our repo for the expense_tracker app.

Let us know if you figure out the source of your problem!

Myron

You must be logged in to comment