【问题标题】:Rails Integration Test With Capybara is Filling In Form with Values from Previous Test使用 Capybara 进行 Rails 集成测试正在使用先前测试中的值填写表格
【发布时间】:2012-01-20 22:57:19
【问题描述】:

我正在使用 test/unit 和 capybara 为 Rails 应用程序运行测试。我对rails相当陌生,所以我希望我遗漏了一些明显的东西。我有以下集成测试来填写单个字段并提交表单。这按预期工作并且测试通过:

  test "create post with title only should add post" do
    assert_difference('Post.count') do
      visit '/posts/new'
      fill_in :title, :with => "Sample Post"
      click_button 'create Post'
    end

    assert current_path == post_path(Post.last)
    assert page.has_content?("Sample Post")
  end

我添加了第二个测试,它几乎复制了前一个测试,但也填写了第二个字段并检查额外的输入(失败)。

  test "create post with title and body should add post" do
    assert_difference('Post.count') do
    visit '/posts/new'
      fill_in :title, :with => "Testing"
      fill_in :body, :with => "This is a sample post"
      save_and_open_page
      click_button 'create Post'
    end
    save_and_open_page
    assert current_path == post_path(Post.last)
    assert page.has_content?("Testing")
    assert page.has_content?("This is a sample post")
end

当失败时,我将调用添加到:

save_and_open_page

并发现表单是用上一次测试的标题值填写的,根本没有提供正文值。测试的名称和断言与第二个测试匹配,因此这不是错误身份的情况。 Capybara 似乎没有获得更新的值。我的 test_helper.rb 文件中也有这段代码:

DatabaseCleaner.strategy = :truncation

module ActionController
  class IntegrationTest
    include Capybara::DSL

    self.use_transactional_fixtures = false

    teardown do
      DatabaseCleaner.clean
      Capybara.reset_sessions!
      Capybara.use_default_driver
    end
  end
end

我假设这应该清除测试之间的值。由于这显然没有发生,我还尝试添加对 Capybara.rest_sessions 的调用!在第一次测试结束时并没有帮助。

任何建议或帮助将不胜感激。

【问题讨论】:

    标签: ruby-on-rails integration-testing capybara


    【解决方案1】:

    我想通了。我使用符号 :title 而不是字段 id 的字符串调用填充方法。我需要使用'post_title'。我是这样开始的,但没有在名称前加上型号名称,所以没有找到它,当我更改为我在我的 erb 代码中使用的符号时它开始工作。

    所以使用:

    fill_in 'post_title', :with => "whatever"
    

    而不是

    fill_in :title, :with => "whatever"
    

    【讨论】:

    • 我计划(并且只是这样做了),我只是被允许这样做,因为您需要等待两天才能标记自己的答案......当我早些时候尝试这样做时,它告诉我我不得不再等2个小时。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-28
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    • 1970-01-01
    • 2014-04-04
    相关资源
    最近更新 更多