Rails 路由在 rspec before(:all) 过滤器中未定义?

发布于 2024-11-28 00:35:05 字数 1009 浏览 1 评论 0原文

我正在使用 capybara 和 rspec 设置一些集成测试。

在单个测试中,这是有效的:

describe "SIGN IN, POST post" do

  it "redirects to /posts after creating a new post" do
    visit new_artist_session_path
    fill_in 'Email', :with => '[email protected]'
    fill_in 'Password', :with => 'password'
    click_link_or_button 'artist_submit'

    visit "/artists/vargas/posts"
    page.should have_content("Upload")
    click_button 'Upload'
    URI.parse(current_url).path.should == "/artists/vargas/posts"
  end
end    

但是,我想将“sign_in”部分移动到 before(:all) 过滤器块,以便我可以干燥我的测试。然而,在 before(:all) 块中,相同的代码似乎给出了此错误:

Failure/Error: visit new_artist_session_path
   NameError:
     undefined local variable or method `new_artist_session_path' for #<RSpec::Core::ExampleGroup::Nested_1:0x0000010399e388>

似乎在 before 块中无法使用路由 url 帮助程序?我该如何补救?

I'm setting up some integration tests using capybara and rspec.

In a single test, this works:

describe "SIGN IN, POST post" do

  it "redirects to /posts after creating a new post" do
    visit new_artist_session_path
    fill_in 'Email', :with => '[email protected]'
    fill_in 'Password', :with => 'password'
    click_link_or_button 'artist_submit'

    visit "/artists/vargas/posts"
    page.should have_content("Upload")
    click_button 'Upload'
    URI.parse(current_url).path.should == "/artists/vargas/posts"
  end
end    

However, I want to move the "sign_in" portion to a before(:all) filter block so that I can DRY up my tests. However it seems that within the before(:all) block, the same code gives this error:

Failure/Error: visit new_artist_session_path
   NameError:
     undefined local variable or method `new_artist_session_path' for #<RSpec::Core::ExampleGroup::Nested_1:0x0000010399e388>

It seems that the routes url helpers are not available from within the before block? How do I remedy this?

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

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

发布评论

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

评论(1

断桥再见 2024-12-05 00:35:05

看来路径助手仅在“it”和“do”之间可用。只是不在 before(:all) 方法中。因此,我最终只是创建了一种登录方法,并将其包含在每个需要它的测试中。

it appears that the paths helpers are available only between the "it" and "do". Just not in the before(:all) method. So I ended up just creating a method for signing in and included it in each test that needed it.

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