为什么我的测试不适用于此表单?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您有
before_filters
,请求可能无法到达控制器操作。检查日志以确保发布正确的参数并且操作未重定向。另一种选择是将其包含在您的规范中:
它会在浏览器中打开当前页面,以便您可以看到实际呈现的内容。
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:
which opens the current page in the browser so you can see what is actually being rendered.