2012-07-29 80 views
1

我正在開發這個Rails 3.2應用程序,使用Apartment gem作爲中間件。應用程序本身完美工作,並且所有RSpec示例在單獨運行時也可以很好地工作。但是,當我使用bundle exec rspec命令同時運行所有測試時,有兩個示例在兩個不同的控制器規格中失敗,並且它們執行完全相同的操作。這裏是有問題的兩個例子:Rails 3 + PostgreSQL + RSpec:應用工作正常,但RSpec示例失敗

issues_controller_spec.rb文件:

describe "GET 'new'" do 

    # ... 

    context "for authenticated users" do 
    before(:each) do 
     controller.log_in(create(:user)) 
     get :new 
    end 

    # ... 

    it "should create a new issue instance and put it in an instance variable" do 
     assigns(:issue).should be_an_instance_of Issue 
    end 
    end 
end 

users_controller_spec.rb文件:

describe "GET 'new'" do 

    # ... 

    context "for authenticated users" do 

     # ... 

    context "for admin users" do 
     before(:each) do 
     admin = create(:admin) 
     admin.add_role :admin 
     controller.log_in(admin) 
     get :new 
     end 

     # ... 

     it "should create a new User instance and put it in an instance variable" do 
     assigns(:user).should be_an_instance_of User 
     end 
    end 
    end 
end 

這兩個例子都是由前面的勾影響:

before(:each) do 
    client = create(:client) 
    @request.host = "#{client.account_name}.lvh.me" 
end 

創建新客戶端時,有一個after_create回調:

# Create the client database (Apartment) for multi-tenancy 
def create_client_database 
    begin 
    Apartment::Database.create(self.account_name) 
    rescue Apartment::SchemaExists 
    return 
    rescue 
    self.destroy 
    end 
end 

而且這裏有例子失敗的地方。現在,如果我刪除begin...rescue...end塊,並保持我得到的failling例子以下異常的行Apartment::Database.create(self.account_name)

ActiveRecord::StatementInvalid: 
    PG::Error: ERROR: current transaction is aborted, commands ignored until end of transaction block 
    : SET search_path TO public 

再次,如果我單獨運行的例子,他們通過,但如果我運行所有的例子,這兩個上面的例子失敗。

有人知道我在做什麼錯嗎?

注意:整個應用程序代碼可以找到here

+0

你究竟在哪裏調用你的控制器動作? – sevenseacat 2012-07-29 14:31:09

+0

@Karpie在另一個[hook之前](https://github.com/AzizLight/AgileBaboon/blob/rolify/spec/controllers/issues_controller_spec.rb#L84),但它並不重要,因爲在這之前的示例失敗(在第一個過濾器之前,在客戶端創建的地方) – 2012-07-29 14:40:56

+0

我們沒有看到調用該操作的代碼,我們沒有看到該錯誤,我們能看到這實際上有幫助嗎? – sevenseacat 2012-07-29 14:42:34

回答

0

我通過包裝線client = create(:client)beginrescueend塊像這樣解決了這個問題:

before(:each) do 
    begin 
    client = create(:client) 
    rescue 
    client = Client.create!(attributes_for(:client)) 
    end 

    @request.host = "#{client.account_name}.lvh.me" 
end 

我不知道如何或爲何這工作,但我知道它的工作原理。