如何在 RSpec 集成测试中发布嵌套属性?

发布于 2024-12-26 03:55:48 字数 784 浏览 1 评论 0原文

我正在编写 RSpec 集成测试,因为我将意大利面条代码转换为使用 accepts_nested_attributes_for。我有这样的片段:

# file: spec/requests/wizard_spec.rb
describe 'POST /wizard with address' do
  before(:each) do
    @premise_attributes = {
      "address"=>"600 Mellow Ave, Mellow Park, CA 94025, USA", 
    }
  end
  it 'should succeed' do
    post :create, "wizard" => { "premise_attributes" => @premise_attributes }
    response.status.should be(200)
  end
end

当然,这会失败:

 Failure/Error: post :create, "wizard" => { "premise_attributes" => @premise_attributes }
 ArgumentError:
   bad argument(expected URI object or URI string)

是否有一种方法可以将嵌套属性哈希转换为可 POST 格式?

(相关但不太重要:post 方法在哪里记录或定义?我想看看它真正接受什么作为参数。)

I'm writing RSpec integration tests as I convert my spaghetti code to use accepts_nested_attributes_for. I have a snippet like this:

# file: spec/requests/wizard_spec.rb
describe 'POST /wizard with address' do
  before(:each) do
    @premise_attributes = {
      "address"=>"600 Mellow Ave, Mellow Park, CA 94025, USA", 
    }
  end
  it 'should succeed' do
    post :create, "wizard" => { "premise_attributes" => @premise_attributes }
    response.status.should be(200)
  end
end

Of course, this fails with:

 Failure/Error: post :create, "wizard" => { "premise_attributes" => @premise_attributes }
 ArgumentError:
   bad argument(expected URI object or URI string)

Is there a method that converts the nested attributes hashes into a POST-able format?

(Related but less important: where is the post method documented or defined? I'd like to see what it really accepts as arguments.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逐鹿 2025-01-02 03:55:48

相反,post :create 尝试使用 post "/wizard" 或将您的规范嵌套在 describe WizardController do 中;结束块。通常,仅当您位于给定控制器的描述块内时,您才可以使用 method :action 语法。

Instead post :create try use post "/wizard" or nest your specs inside describe WizardController do; end block. Generally you can use method :action syntax only if you're inside describe block for the given controller.

薯片软お妹 2025-01-02 03:55:48

我在尝试测试 put 时发现了这一点。我的 post 方法虽然有效,所以如果您仍然需要它,也许我可以帮助您。我认为你的问题是你试图更新你的属性,就好像它是标量类型变量一样,但嵌套属性实际上就像一个数组。 Rails 通常将它们命名为“0”、“1”等,但我不确定名称是什么,只要它们是唯一的即可。尝试一下:(

@premise_attributes = {
  "0" => {"address"=>"600 Mellow Ave, Mellow Park, CA 94025, USA"}
}

顺便说一句,我遇到的问题是我的 update 规范失败了,因为它说我的地址之类的内容对于借用您的示例并不唯一。)

I found this while trying to fix my issue with trying to test put. My post method works though so maybe I can help you out if you still need it. I think your issue is that you're trying to update your attributes as if it was a scalar type variable, but nested attributes are really like an array. Rails generally names them "0", "1", etc., but I'm not sure it matters what the names are as long as they're unique. Give this a try:

@premise_attributes = {
  "0" => {"address"=>"600 Mellow Ave, Mellow Park, CA 94025, USA"}
}

(By the way, the problem I'm having is that my update specs are failing because it says something like my address is not unique to borrow your example.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文