如何使用 RSpec 3.12 检查记录停用?

How to check a record de-activation with RSpec 3.12?

提问人:user1185081 提问时间:11/7/2023 更新时间:11/7/2023 访问量:22

问:

My Playground 控制器并没有真正删除记录,如果有人想要删除 Playground,它会将它们标记为非活动状态:

def destroy
  @playground.set_as_inactive(current_login)
  respond_to do |format|
    format.html { redirect_to playgrounds_url, notice: t('.Success') } #'Playground was successfully deleted.'
    format.json { head :no_content }
  end
end

其中set_as_inactive方法只是将 is_active 标志设置为 false,并跟踪执行更改的人员。

此功能在应用程序中按预期工作,但是当我尝试在 Playgrounds 请求中测试它时,is_active标志似乎未设置为 false:

  describe "DELETE /destroy" do
    let(:playground_tbd) { create(:playground) }
    it "destroys the requested playground" do
      expect {
        delete playground_url(playground_tbd)
      }.to change(Playground.visible, :count).by(-1) 
      expect(response).to have_http_status 302
      expect(response).to redirect_to(playgrounds_url)
    end
    it "renders the expected status" do
      get playground_url(playground_tbd)
      expect(playground_tbd.is_active).to eq(false)
    end
  end

我的解释:

  1. playground_tbd是专门为此测试创建的,之后将进行删除
  2. Playground.visible 仅返回活动的游乐场 - 计数已更改(删除 1 个),这让我认为测试确实有效
  3. 当我重新加载并检查playground_tbd时,它说is_active是真的?!

我在这里错过了什么?

Ruby-on-Rails rspec

评论

2赞 dbugger 11/7/2023
测试是单独运行的 -- 您的第二个测试是针对处于原始状态的记录运行的 -- 因此iis_active为 true。您需要在测试中删除它才能获得预期的行为。
0赞 Les Nightingill 11/7/2023
我认为语法应该是 parens,而不是括号{Playground.visible.count}
2赞 engineersmnky 11/7/2023
@dbugger说了什么! 在“销毁请求的游乐场”中与在“呈现预期状态”中不同。测试被设计为以任何顺序独立运行,因此每个设置(通常)都应该能够独立运行。playground_tbdplayground_tbd
1赞 user1185081 11/7/2023
您的意思是 let 指令对描述块中的每个测试都重复吗?然后我添加了 2 行: - 删除 playground_url(playground_tbd) - 和: - playground_tbd.reload - 它有效!多谢!
1赞 dbugger 11/7/2023
let 语句在每次测试中都是新运行的。

答: 暂无答案