RSpec控制器错误:数据库自行填充

时间:2015-11-10 06:31:24

标签: ruby-on-rails rspec

提前感谢任何想要帮助的人。我的开发数据库是空的,但仍然在测试它是29个作者填充数据库,它应该只有两个,如果我出错了,请帮忙

(规格/控制器/ authors_controller.rb)

x = ['i like cats', 'i like dogs', 'i like both']
for position in range(len(x)):
    if 'both' in x[position]:
        print ('.'.join([ele for ele in x[0:2]]) if position==0 else '.'.join([ele for ele in x[position-1:position+2]]))

(应用程序/控制器/ authors_controller.rb)

it "loads all of the authors into @authors" do
  auth1, auth2 = Fabricate(:author), Fabricate(:author)
  get :index

  expect(assigns(:authors)).to match_array([auth1, auth2])
end

错误

def index
  @authors = Author.all
end

1 个答案:

答案 0 :(得分:1)

正如@sevenseacat指出的那样,你不是在规范之间清理你的数据库。 DatabaseCleaner gem会为你做这件事。安装完成后,请务必将以下内容放入spec_helper

RSpec.configure do |config|

  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
  end

  config.around(:each) do |example|
    DatabaseCleaner.cleaning do
      example.run
    end
  end

end