Rspec控制器 - 数据库中的对象持久性/作为测试构建的实例

时间:2016-03-01 17:49:30

标签: ruby-on-rails rspec rspec-rails

我习惯于测试这样的建筑物:

describe "post to refund first time" do
  it "should have 1 refund" do
    post :refund
    Refund.count.should == 1
  end

  describe "post to refund 2nd time" do
    it "should have 2 refunds" do
      post :refund
      Refund.count.should == 1
    end
  end
end

除了规范2失败,Refund.count只有1.我插入binding.pry,确实从内存中清除了第一笔退款。这是Rspec控制器测试的正常行为还是我做错了什么?

1 个答案:

答案 0 :(得分:0)

确实Rails和RSpec在每次运行时重置数据库。为了让你的测试工作,你必须真正击中端点2次:

describe "post to refund first time" do
  it "should have 1 refund" do
    post :refund
    Refund.count.should == 1
  end

  describe "post to refund 2nd time" do
    it "should have 2 refunds" do
      2.times { post :refund }
      Refund.count.should == 2
    end
  end
end