【发布时间】:2017-06-21 23:47:16
【问题描述】:
我刚刚开始使用 Rails,我正在使用 RSpec 为 Todo 列表应用程序编写一些单元测试。我已经编写并运行了 REST API,但现在我似乎无法通过测试找出问题。这是规格:
require 'rails_helper'
describe "Lists API" do
context "from start" do
it 'is empty' do
get "/lists"
expect(response).to be_success
expect(json.size).to eq(0)
end
it 'can create Lists' do
post "/lists", :list => {:title => "First List", :status => "Unstarted"}
expect(response).to be_success
post "/lists", :list => {:title => "Second List", :status => "Unstarted"}
expect(response).to be_success
#lines only here to show the problem only exists in a different test block
get "/lists"
expect(response).to be_success
expect(json.size).to eq(2)
end
end
context "once populated" do
it 'can view created lists' do
get "/lists"
expect(response).to be_success
expect(json.size).to eq(2)
end
end
end
然后当我运行 RSpec 时,我得到这个错误:
Failures:
1) Lists API once populated can view created lists
Failure/Error: expect(json.size).to eq(2)
expected: 2
got: 0
(compared using ==)
似乎每个it 块的数据库都被清空了。那是对的吗?有什么办法可以为每个describe 创建一个新的数据库,但没有为每个it 清空它?
【问题讨论】:
标签: ruby-on-rails rspec