为什么Capybara无法看到此页面的内容?

Nat*_*ong 4 rspec ruby-on-rails capybara

我正在尝试使用Capybara编写Rspec请求规范.似乎一切都正常,除了 Capybara将页面视为空白.

好迹象:

  • 页面在浏览器中加载正常
  • 拖尾日志表明在测试运行期间呈现了正确的视图
  • 我执行了在视图中放置的任何日志记录语句
  • 如果我使用assert_select "h1", :text => "hello world",测试通过.

不好的迹象:

  • 如果我使用page.should have_content('hello world'),它会失败,说Capybara::ElementNotFound: Unable to find xpath "/html"
  • 如果我这样做$stdout.puts page.html,除了doctype之外它是空的

我的测试看起来像这样:

describe "working with foos" do
  it "should have a 'new foo' form" do
    get '/foos/new'
    assert_select 'h1', text: 'hello world' # passes
    page.should have_content('hello world') # fails
    $stdout.puts page.html                  # empty except for a doctype
  end
end
Run Code Online (Sandbox Code Playgroud)

这可能有什么问题?

Nat*_*ong 7

额头拍打的答案

...我正在分享以拯救他人同样的痛苦.

Capybara使用该visit方法设置其页面变量.

visit '/assembly/manage/task_lists/new'
Run Code Online (Sandbox Code Playgroud)

不要get与Capybara一起使用:

get '/assembly/manage/task_lists/new'
Run Code Online (Sandbox Code Playgroud)