在测试 Rails 控制器时,如何将嵌套资源与多个 post 值混合?
我有一个名为“Post”的模型,它是一个名为“Project”的模型下的嵌套资源,我正在尝试测试控制器。该代码可以在我的浏览器中找到,但我无法让它在测试中工作。这是测试
context "on POST to :edit" do
setup do
post( :edit,
:project_id => @project1.to_param,
:id => @post1.to_param,
:post => { :title => 'new title', :text => 'other text' }
)
end
should_assign_to :post
should_assign_to :project
should_respond_with :success
should "update post values" do
assert_equal 'other text', assigns['post'].text
end
你知道我是如何搞砸的吗?
I have a model called "Post" which is a nested resource under a model called "Project" and I'm trying to test the controller. The code works find in my browser, but I can't get it to work in the test. Here's the test
context "on POST to :edit" do
setup do
post( :edit,
:project_id => @project1.to_param,
:id => @post1.to_param,
:post => { :title => 'new title', :text => 'other text' }
)
end
should_assign_to :post
should_assign_to :project
should_respond_with :success
should "update post values" do
assert_equal 'other text', assigns['post'].text
end
Any idea how I'm screwing this up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为我不理解 Rails 的 REST 架构 - 或者 - 后语法。我应该使用 PUT 而不是 POST,并且调用应该如下所示:
我一直使用错误的语法,因为由于某种原因它仍然正确处理我的嵌套 id。
This was the result of me not understanding Rails' REST architecture -or- the post syntax. I should have been using PUT instead of POST, and the call should have looked like this:
I had been using the wrong syntax because for some reason it was still handling my nested id's correctly.