【问题标题】:Why isn't my tests working for this form?为什么我的测试不适用于此表单?
【发布时间】:2011-09-27 13:39:10
【问题描述】:

在 Firefox 中,如果我尝试提交没有标题的帖子,我会收到:1 错误禁止保存此帖子

但是当我运行我的测试时。这是一个不同的故事。

我的帖子模型有validates_presence_of :title。我的测试看起来像:

require 'spec_helper'

describe 'Users' do

  it 'registered users should not be able to post without a title', :js => true do
    user = Factory(:user)
    visit new_post_path
    current_path.should eq(new_post_path)

    fill_in 'post[markdown_description]', :with => 'Bar'
    click_on 'Submit your post'

    page.should have_content('error')
  end

end

顺便说一句,我使用的是 Selenium (:js => true),因为我的提交按钮实际上是一个带有一些 JS 的锚链接。基本上,当点击链接时,JS会触发表单提交。

Rspec 返回:

Running: spec/requests/users_spec.rb
F

Failures:

  1) Users registered users should be able to post
     Failure/Error: page.should have_content('error')
       expected there to be content "error" in ""
     # ./spec/requests/users_spec.rb:13:in `block (2 levels) in <top (required)>'

Finished in 7.9 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/requests/users_spec.rb:4 # Users registered users should be able to post

【问题讨论】:

    标签: ruby-on-rails-3 testing selenium rspec capybara


    【解决方案1】:

    如果您有before_filters,该请求可能无法到达控制器操作。检查日志以确保发布的参数正确且操作未重定向。

    另一种选择是将其包含在您的规范中:

    click_on 'Submit your post'
    save_and_open_page
    

    在浏览器中打开当前页面,以便您查看实际呈现的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-24
      • 1970-01-01
      • 1970-01-01
      • 2021-04-17
      • 2020-06-06
      • 1970-01-01
      • 1970-01-01
      • 2018-05-05
      相关资源
      最近更新 更多