【问题标题】:Updated to Capybara 2.1.0.rc, now having trouble with current_path in my feature specs更新到 Capybara 2.1.0.rc,现在我的功能规范中的 current_path 有问题
【发布时间】:2013-05-16 06:43:50
【问题描述】:

看来我在request specs 中多次使用current_path。但是,Capybara 现在要求规格在 features directory 中才能使用 Capybara DSL (page & visit)

# creating_posts_spec.rb

require "spec_helper"

feature "creating posts" do

  scenario "creating posts with valid info is successful" do
    visit new_post_path
    fill_in 'Title', with: "This is a title test"
    fill_in 'Content', with: "This is a content test"
    click_button 'Create Post'
    page.should have_content 'Post was successfully created.'
    page.current_path.should == post_path(post)
  end
end

它是一个简单的帖子控制器,并且在帖子在浏览器中呈现后页面就会显示出来。

 posts GET    /posts(.:format)               posts#index
                     POST   /posts(.:format)               posts#create
            new_post GET    /posts/new(.:format)           posts#new
           edit_post GET    /posts/:id/edit(.:format)      posts#edit
                post GET    /posts/:id(.:format)           posts#show
                     PUT    /posts/:id(.:format)           posts#update
                     DELETE /posts/:id(.:format)           posts#destroy
                root        /                              posts#index

为什么我收到错误消息:undefined local variable or method 'post'?

我只是不记得曾经遇到过这种麻烦。 有人有建议或理由吗?还是我错过了一些明显的东西?来晚了

【问题讨论】:

  • 感谢您的建议,但我已经完成了所有这些,page 工作正常。它只有current_path 行。这很奇怪,除非我遗漏了一些明显的东西。

标签: ruby-on-rails tdd capybara acceptance-testing


【解决方案1】:

因为post是未定义....你没有在整个场景中的任何地方设置它。

【讨论】:

  • 如果你每次都清空你的数据库,你可能会得到类似post_path(1)的东西,以保存查找并保持你的测试快速。
  • 你的权利,我应该做类似post = Post.find_by_title("This is a title test")的事情。我知道我遗漏了一些明显的东西。
  • 我猜功能规格对他们有不同的感觉。我在想像你刚刚提出的东西,而且是对的,看起来更像是请求规范。无论如何,感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多