如何在 RSpec 集成测试中发布嵌套属性?
我正在编写 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
相反,
post :create
尝试使用post "/wizard"
或将您的规范嵌套在describe WizardController do 中;结束
块。通常,仅当您位于给定控制器的描述块内时,您才可以使用method :action
语法。Instead
post :create
try usepost "/wizard"
or nest your specs insidedescribe WizardController do; end
block. Generally you can usemethod :action
syntax only if you're inside describe block for the given controller.我在尝试测试
put
时发现了这一点。我的post
方法虽然有效,所以如果您仍然需要它,也许我可以帮助您。我认为你的问题是你试图更新你的属性,就好像它是标量类型变量一样,但嵌套属性实际上就像一个数组。 Rails 通常将它们命名为“0”、“1”等,但我不确定名称是什么,只要它们是唯一的即可。尝试一下:(顺便说一句,我遇到的问题是我的
update
规范失败了,因为它说我的地址之类的内容对于借用您的示例并不唯一。)I found this while trying to fix my issue with trying to test
put
. Mypost
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:(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.)