首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Rspec测试和attr_accessor内部设置FactoryGirl值

从Rspec测试和attr_accessor内部设置FactoryGirl值
EN

Stack Overflow用户
提问于 2013-12-02 02:58:12
回答 2查看 3.5K关注 0票数 2

我正在使用这个Rails创业板在我的评论表单中添加captcha问题。https://github.com/kiskolabs/humanizer

我正试图在我的Rspec测试中绕过captcha。根据文档,这可以通过将这些代码添加到我的模型中来完成。

代码语言:javascript
复制
attr_accessor :bypass_humanizer
require_human_on :create, :unless => :bypass_humanizer

现在,我只需要知道如何在测试中设置'bypass_humanizer‘属性。这是我目前测试的内容:

代码语言:javascript
复制
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测试中这样做,并且测试将通过。

代码语言:javascript
复制
hello = create(:comment, body: 'Hello World', bypass_humanizer: true)

所以我只需要为FactoryGirl attributes_for找到类似的东西

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-12-02 11:47:11

您可以尝试在您的bypass_humanizer true工厂中添加FactoryGirl,这样每次生成该类的实例时,bypass_humanizer都设置为true。

作为替代,您可以以这种方式编写测试:

代码语言:javascript
复制
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
票数 1
EN

Stack Overflow用户

发布于 2013-12-04 19:37:41

您需要确保控制器和模型中允许“bypass_humanizer”。

如果您正在使用strong_parameters,请确保它在控制器的白名单中。

如果您使用的是attr_accessible,请确保它是适当列出的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20320156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档