문제

Recently, I've started diving into Ruby MVCs, in order to find the best, fastest, most minimal framework to build my app. Being unsatisfied with Rails, I decided to try out Padrino. I'm also trying out Outside-in TDD for a full app for the first time, so being able to write tests for all components is critical. Unfortunately, I cannot get past making models in Padrino, so I'm wondering if it's just a cause of beta software, or just error on my part.

I start off by creating my project with Cucumber and RSpec for testing and Sequel for my ORM.

$ padrino g project test -d sequel -t cucumber -c sass -b

Next, I create some model and migration:

$ padrino g model user

# ./db/migrate/001_create_users.rb
Sequel.migration do
  change do
    create_table :users do
      primary_key :id
      String :name
      String :password
    end
  end
end

Next, of course, comes the spec. For sake of example, just something simple:

# ./spec/models/user_spec.rb
require 'spec_helper'

describe User do
  it 'can be created' do
    user = User.create
  end
end

Now, migrate and run the spec:

$ padrino rake sq:migrate:up
$ rspec spec

F

Failures:

  1) User can be created
     Failure/Error: user = User.create
     Sequel::DatabaseError:
       SQLite3::SQLException: no such table: users
     # ./spec/models/user_spec.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.00169 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/models/user_spec.rb:4 # User can be created

This is very confusing. It was at this point that I thought going into the Padrino console would help me solve this strange issue. I was wrong.

$ padrino c
> User.create
  => #<User @values={:id=>1, :name=>nil, :password=>nil}>
> User.all
  => [#<User @values={:id=>1, :name=>nil, :password=>nil}>]

Fair enough results, but then I try it with the test environment:

$ padrino c -e test
> User.create
  Sequel::DatabaseError: SQLite3::SQLException: no such table: users

I know that in RoR, to get integrated models to run, you have to do something like rake db:test:prepare, but doing padrino rake -T doesn't seem to reveal any equivalent (tested with Sequel, DataMapper, and ActiveRecord; none seem to have the db:test:prepare). So, my question is: how do I get integrated database tests running within Padrino?

도움이 되었습니까?

해결책

From this forum post, I discovered why db:test:prepare is this weird, arbitrary rake task one needs to run: it has to do with the test environment (versus development and production). In Padrino, this translates to the following code, which is a bit more obscure, but more intuitive as well:

$ padrino rake sq:migrate:up -e test

This tells Padrino to create the table for the test database, which allows the spec(s) to pass.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top