2012-07-29 133 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 

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