为什么我的测试不适用于此表单?

发布于 2024-12-07 02:03:52 字数 1158 浏览 1 评论 0原文

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

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

我的 Post 模型有 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

In Firefox, if I try to submit a post without a title, I get: 1 error prohibited this post from being saved.

But when I run my test. It's a different story.

My Post model has validates_presence_of :title. My test looks like:

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

By the way, I am using Selenium (:js => true), because my submit button is actually an anchor link with some JS. Basically, when the link is clicked, JS triggers the form to be submitted.

Rspec returns:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回心转意 2024-12-14 02:03:52

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

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

click_on 'Submit your post'
save_and_open_page

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

The request may not make it to the controller action if you have before_filters. Check the log to make sure that the correct parameters are posting and that the action is not redirecting.

Another option is to include this in your spec:

click_on 'Submit your post'
save_and_open_page

which opens the current page in the browser so you can see what is actually being rendered.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文