我正在使用 Apartment gem 作为中间件开发这个 Rails 3.2 应用程序。应用程序本身运行良好,所有 RSpec 示例在单独运行时也运行良好。但是,当我使用该命令同时运行所有测试时bundle exec 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 钩子的影响:
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
块并保留该行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
同样,如果我单独运行示例,它们会通过,但如果我运行所有示例,则上面的两个示例会失败。
有谁知道我做错了什么?
注意:整个应用程序代码可以在这里找到。