在Rails中进行功能测试 [英] Making functional tests in Rails with Devise

查看:142
本文介绍了在Rails中进行功能测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天三年的拖延是我开始测试我的Rails应用程序的那一天。我的第一步是修复我的Rails 3 beta4应用程序中的失败测试。

After 3 years of procrastination today is the day that I start testing my Rails apps. My first step is to fix the failing tests in my Rails 3 beta4 app.

我最后3次失败的测试与设计宝石及其authenticate_user有关方法在我的控制器顶部的before_filter中。

My last 3 failing tests have to do with the devise gem and its authenticate_user! method in a before_filter at the top of my controller.

通过帮助我,可以帮助我,因为这将使我能够从现在开始使用TDD方法

You'd earn great karma by helping me out with this since it will enable me to use the TDD methodology from now on.

以下是我遇到的错误:

1) Error:
test_should_get_accepted(ModerationControllerTest):
NoMethodError: undefined method `authenticate!' for nil:NilClass
    /test/functional/moderation_controller_test.rb:10:in `test_should_get_accepted'

Devise只是在这个页面给出功能测试指针和帮助: http://github.com/plataformatec/devise ,但我只是不知道如何把它应用到。

Devise just gives functional tests pointers and helpers in this page: http://github.com/plataformatec/devise but I just don't know how to put this into application.

你可以给这个测试noob一些关于如何使用这些帮助者的详细说明吗?

Can you please give this testing noob some detailed instructions on how to use these helpers?

推荐答案

有一段时间,但我找到了方式。在这里,有人卡在同一点上:

It took me a while but I found the way. Here it is for anyone stuck at the same point:

在moderation_controller_test.rb的顶部,在类声明之下,添加以下行:

At the top of the moderation_controller_test.rb, below the class declaration, add this line:

include Devise::TestHelpers

我的用户夹具中有2条记录,我在每个测试中添加了这个行,用户必须被授权执行该操作。

I have 2 records in my user fixture and I added this line within each test where the user has to be authorized to perform the action.

sign_in User.first

当然,一旦你知道如何做,这是很简单的。

Of course it's dead simple once you know how to do it.

这篇关于在Rails中进行功能测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆