2013-03-09 62 views
0

我正在與本書面向服務的設計與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 => "[email protected]", 
     :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 

運行耙分貝後:遷移

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... 
+1

通常測試數據庫不同於您的開發數據庫。確保你創建了你的測試數據庫 – 2013-03-09 04:50:31

回答

0

migrate任務調用environment爲前提,其設置默認的環境,「發展」,除非有一個變量ENV說別的東西。

發生什麼事是你的開發數據庫被遷移,但不是你的測試數據庫。

喜歡你的書的說明說,過了一會兒,叫

rake db:migrate SINATRA_ENV=test 

,它會成爲你測試 DB被遷移。

+0

本書的這個版本並沒有這麼說(至少不是我能看到的),但是謝謝 – BrainLikeADullPencil 2013-03-09 06:12:28

+0

https://github.com/pauldix/service-oriented-design-with-ruby/blob/master/chapter_01/README#L35 – 2013-03-09 15:29:40

+0

另一種選擇是'RACK_ENV = test rake db:migrate' – equivalent8 2014-12-10 09:37:38

相關問題