【发布时间】: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