我正在使用 Ruby 和 Rails 的面向服务的设计一书。在第一章中,它使用活动记录创建了一个带有 Sinatra 应用程序的服务。我创建了活动记录迁移(001_create_users.rb,见下文),并运行 rake 文件来迁移它(使用命令rake db:migrate
),它给我的输出似乎表明用户表已创建(见这篇文章的底部运行 rake db:migrate 后的输出)。但是,当我运行规范(rspec spec/service_spec.rb
见下文)时,它给了我一个错误,说它找不到用户表。
table_structure': Could not find table 'users' (ActiveRecord::StatementInvalid)
你能告诉我我可能做错了什么吗?本书第一章的源代码也可以在这里找到https://github.com/pauldix/service-oriented-design-with-ruby/tree/master/chapter_01
规范/service_spec.rb
RSpec.configure do |conf|
conf.include Rack::Test::Methods
end
def app
Sinatra::Application
end
describe "service" do
before(:each) do
User.delete_all
end
describe "GET on /api/v1/users/:id" do
before(:each) do
User.create(
:name => "paul",
:email => "paul@pauldix.net",
:password => "strongpass",
:bio => "rubyist")
end
it "should return a user by name" do
get '/api/v1/users/paul'
last_response.should be_ok
attributes = JSON.parse(last_response.body)["user"]
attributes["name"].should == "paul"
end
/db/migrate/001_create_users.rb
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :name
t.string :email
t.string :password
t.string :bio
t.timestamps
end
end
def self.down
drop_table :users
end
end
耙文件
require 'rubygems'
require 'active_record'
require 'yaml'
require 'logger'
desc "Load the environment"
task :environment do
env = ENV["SINATRA_ENV"] || "development"
databases = YAML.load_file("config/database.yml")
ActiveRecord::Base.establish_connection(databases[env])
end
namespace :db do
desc "Migrate the database"
task(:migrate => :environment) do
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Migration.verbose = true
ActiveRecord::Migrator.migrate("db/migrate")
end
end
运行 rake db:migrate
D, [2013-03-08T20:11:55.490937 #3824] DEBUG -- : (0.2ms) select sqlite_version(*)
D, [2013-03-08T20:11:55.493906 #3824] DEBUG -- : (2.1ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
D, [2013-03-08T20:11:55.497176 #3824] DEBUG -- : (2.6ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
D, [2013-03-08T20:11:55.544128 #3824] DEBUG -- : (44.9ms) SELECT "schema_migrations"."version" FROM "schema_migrations"
I, [2013-03-08T20:11:55.544615 #3824] INFO -- : Migrating to CreateUsers (1)
D, [2013-03-08T20:11:55.545263 #3824] DEBUG -- : (0.1ms) begin transaction
== CreateUsers: migrating ====================================================
-- create_table(:users)
D, [2013-03-08T20:11:55.570509 #3824] DEBUG -- : (0.6ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "email" varchar(255), "password" varchar(255), "bio" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
-> 0.0018s
== CreateUsers: migrated (0.0019s) ===========================================
D, [2013-03-08T20:11:55.571113 #3824] DEBUG -- : (0.1ms) INSERT INTO "schema_migrations" ("version") VALUES ('1')
D, [2013-03-08T20:11:55.576187 #3824] DEBUG -- : (4.8ms) commit transaction
michael$ rspec spec/service_spec.rb
D, [2013-03-08T20:12:57.812685 #3854] DEBUG -- : env: test
D, [2013-03-08T20:12:57.910829 #3854] DEBUG -- : db/test.sqlite3 database connection established...