Rails 集成测试:我的测试向控制器发布了一个新条目,如何检索该条目?

Rails integration test: my test posts a new entry to the controller, how do I retrieve this entry?

我不知道如何检索我刚刚发布的测试的新条目。 这是我的代码:

post admin_bundles_path, params: { bundle: {
    title: "New bundle",
    sku: "ASD-FESTA-00001-XM",
} }, xhr: true
assert_response :ok

到目前为止,还不错。现在,如何检查这个新条目的属性?较旧的条目在我的灯具上,因此我可以通过 bundles(:some_bundle) 访问它们。我该如何为新的做?

你可以试试这个:

Bundle.last

这将获得您刚刚 post 编辑的最后一个条目。被警告!如果由于某种原因您的 post 请求失败,那么您实际上是在检索固定装置中的旧条目。

如果你想绝对确定,那么你可以这样做:

Bundle.where(title: "New bundle", sku: "ASD-FESTA-00001-XM").first

如果由于某种原因 post 请求失败,这将产生错误。