我现在正在为API编写一些请求规范。我看到自己对同一类请求做了很多断言。这包括(但不限于)
目前,我正在编写四个it
块 - 每个断言一个。我的每个it
块现在都包含一个post
调用,然后是一个断言。结果,这四个块包含重复的方法调用(我知道我可以将它包装在之前),但更糟糕的是:它会在应用程序中点击4次。
避免对发布和后续请求处理的所有这些重复调用的最简单方法是将4个断言置于同一it
块下。但是,这会牺牲可读性并在运行规范套件时产生不足的输出。
基本上,我正在寻找一种写这样的东西的方法:
context 'products#create' do
context 'success' do
post '/some/api/endpoint', some: 'data', headers: {access_key: 1234}
it 'responds with 200 OK' do
expect(response.status).to eq 200
end
it 'changes the database state' do
expect(some_model.reload.title).to eq 'new title'
end
it 'responds with a JSON representation of the resource' do
expect(JSON.parse(response.body)).to match hash_including(desc: 'ription', price: 1234)
end
# and so on...
end
end
我已尝试将帖子请求放在before(:all)
块中,但这阻止我使用allow(SomeExternalService).to receive(:some_call).and_return('something')
这样做的想法?