如何与 Factory Girl 一起使用“分配”

发布于 2024-11-16 00:27:41 字数 973 浏览 2 评论 0原文

嗨,

我有以下测试:

  test "should annoy Chuck Norris" do
    son = Factory.build(:son)
    assert_difference('Son.count') do
      post :create, son: { asset: son.asset, chuck_id: son.chuck }
    end

    assert_redirected_to chuck_path(assigns(son.chuck))
    assert_equal 'The world has been destroyed...', flash[:notice]
  end

assigns(son.chuck) 不起作用,当我尝试 son.chuck 时,它起作用了,但是在运行了所有测试之后创建一些 ChucksFactory.build(:son) 创建了一个 Chuck,其 ID 为 12断言_重定向_至正在发送 ID 1。导致出现以下错误:

Expected response to be a redirect to <http://test.host/chucks/12> but was a redirect to <http://test.host/chucks/1>
          /Users/edison/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.1.0.rc4/lib/action_dispatch/testing/assertions/response.rb:67:in `assert_redirected_to'

那么我怎样才能得到正确的Chuck.id呢?

Hiho,

I have the following test:

  test "should annoy Chuck Norris" do
    son = Factory.build(:son)
    assert_difference('Son.count') do
      post :create, son: { asset: son.asset, chuck_id: son.chuck }
    end

    assert_redirected_to chuck_path(assigns(son.chuck))
    assert_equal 'The world has been destroyed...', flash[:notice]
  end

assigns(son.chuck) doens't work, when I try son.chuck, it works, but after running all my tests and creating some Chucks, Factory.build(:son) created a Chuck with id 12 and the assert_redirected_to are sending id 1. Resulting the following error:

Expected response to be a redirect to <http://test.host/chucks/12> but was a redirect to <http://test.host/chucks/1>
          /Users/edison/.rvm/gems/ruby-1.9.2-p180/gems/actionpack-3.1.0.rc4/lib/action_dispatch/testing/assertions/response.rb:67:in `assert_redirected_to'

So how can I get the correct Chuck.id?

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

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

发布评论

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

评论(1

墨洒年华 2024-11-23 00:27:41

我认为您想要使用的是assigns(:chuck).son

分配的参数必须与控制器中的变量名称匹配。换句话说,假设控制器中有 @chuck.son,则应该使用 assigns(:chuck).son。请注意参数如何与 @ 符号后面的内容相对应。

I think what you're looking to use is assigns(:chuck).son.

The argument to assigns must match the variable name in the controller. In other words, assuming you have @chuck.son in the controller, you should use assigns(:chuck).son. Note how the argument corresponds to what come right after the @ symbol.

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