我正在使用这个Rails创业板在我的评论表单中添加captcha问题。https://github.com/kiskolabs/humanizer
我正试图在我的Rspec测试中绕过captcha。根据文档,这可以通过将这些代码添加到我的模型中来完成。
attr_accessor :bypass_humanizer
require_human_on :create, :unless => :bypass_humanizer现在,我只需要知道如何在测试中设置'bypass_humanizer‘属性。这是我目前测试的内容:
it 'saves the new comment in the database' do
comment = FactoryGirl.attributes_for(:comment, :bypass_humanizer => true)
expect {
post :create, blog_id: @blog, comment: comment
}.to change(Comment, :count).by(1)
end任何帮助都是非常感谢的。
有趣的是,我可以在我的另一个Rspec测试中这样做,并且测试将通过。
hello = create(:comment, body: 'Hello World', bypass_humanizer: true)所以我只需要为FactoryGirl attributes_for找到类似的东西
发布于 2013-12-02 11:47:11
您可以尝试在您的bypass_humanizer true工厂中添加FactoryGirl,这样每次生成该类的实例时,bypass_humanizer都设置为true。
作为替代,您可以以这种方式编写测试:
it 'saves the new comment in the database' do
comment = FactoryGirl.attributes_for(:comment)
comment.merge!(:bypass_humanizer => true)
expect {
post :create, blog_id: @blog, comment: comment
}.to change(Comment, :count).by(1)
end发布于 2013-12-04 19:37:41
您需要确保控制器和模型中允许“bypass_humanizer”。
如果您正在使用strong_parameters,请确保它在控制器的白名单中。
如果您使用的是attr_accessible,请确保它是适当列出的。
https://stackoverflow.com/questions/20320156
复制相似问题