在Rails中进行功能测试

时间:2021-02-20 03:53:17

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年的拖延,今天是我开始测试我的Rails应用的日子。我的第一步是修复Rails 3 beta4应用中的失败测试。

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.

我的最后3个失败的测试与设计gem和它的authenticate_user有关!方法,在我的控制器顶部的before_filter中。

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

通过帮助我解决这个问题,您将获得巨大的业力,因为它将使我从现在开始使用TDD方法。

Here is the error that troubles me:

这里有一个错误困扰着我:

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 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.

设计只是在这个页面中提供了功能测试指针和助手:http://github.com/plataformatec/design,但我只是不知道如何将其应用到应用程序中。

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

你能给这个测试noob一些关于如何使用这些助手的详细说明吗?

5 个解决方案

#1


50  

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

我花了一段时间才找到路。这是给那些被困在同一点上的人的:

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

在moderation_controller_test的顶部。rb,在类声明下面,添加以下一行:

include Devise::TestHelpers

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.

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

sign_in User.first

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

当然,一旦你知道怎么做,这就非常简单了。

#2


18  

If you want the Devise test helpers to be available to all of your tests, you have to enclose the include mentioned by allesklar at the bottom of test_helper.rb in a class declaration like this:

如果您想要设计测试助手对所有测试都可用,您必须将allesklar提到的include包含在test_helper的底部。rb的类声明如下:

class ActionController::TestCase
  include Devise::TestHelpers
end

Update: 01.25.2017

更新:01.25.2017

... rails 5 posts a DEPRECATION WARNING & asks you use ...

…rails 5发布了一个弃用警告并要求您使用…

Devise::Test::ControllerHelpers

测试设计::::ControllerHelpers

#3


7  

I'm relatively new to Rails, so I'd like to add a couple things that may not be obvious to other new folks.

我对Rails比较陌生,所以我想添加一些对其他新人来说可能不太明显的东西。

Concerning the user fixture, I had to define one but leave it empty in order for this to work:

关于用户夹具,我必须定义一个,但要让它空着才能工作:

# in users.yml    
joe: {}

When using the devise sign_in helper, you can access the hash object directly in your test:

当您使用designsign_in helper时,您可以在测试中直接访问散列对象:

# a test method in some_controller_test.rb
sign_in users(:joe)

See http://guides.rubyonrails.org/testing.html#the-low-down-on-fixtures for more information on Rails fixtures.

有关Rails fixture的更多信息,请参见http://guides.rubyonrails.org/testing.html#。

#4


1  

Quoting verbatim from https://github.com/plataformatec/devise:

从https://github.com/plataformatec/devise逐字引用:

If you're using RSpec, you can put the following inside a file named spec/support/devise.rb:

如果您正在使用RSpec,您可以将以下内容放入一个名为spec/support/devise的文件中:

RSpec.configure do |config|

RSpec。配置配置| |

config.include Devise::TestHelpers, :type => :controller

配置。包括设计:::TestHelpers,:type =>:controller

end

结束

You can now use sign_in and sign_out in your RSpec tests.

现在可以在RSpec测试中使用sign_in和sign_out。

#5


0  

In addition to the code in the test_helpers.rb, I added this at the top of the controller_test and it worked for me:

除了test_helper中的代码之外。rb,我在controller_test顶部添加了这个,它对我有效:

require 'test_helper'

#1


50  

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

我花了一段时间才找到路。这是给那些被困在同一点上的人的:

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

在moderation_controller_test的顶部。rb,在类声明下面,添加以下一行:

include Devise::TestHelpers

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.

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

sign_in User.first

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

当然,一旦你知道怎么做,这就非常简单了。

#2


18  

If you want the Devise test helpers to be available to all of your tests, you have to enclose the include mentioned by allesklar at the bottom of test_helper.rb in a class declaration like this:

如果您想要设计测试助手对所有测试都可用,您必须将allesklar提到的include包含在test_helper的底部。rb的类声明如下:

class ActionController::TestCase
  include Devise::TestHelpers
end

Update: 01.25.2017

更新:01.25.2017

... rails 5 posts a DEPRECATION WARNING & asks you use ...

…rails 5发布了一个弃用警告并要求您使用…

Devise::Test::ControllerHelpers

测试设计::::ControllerHelpers

#3


7  

I'm relatively new to Rails, so I'd like to add a couple things that may not be obvious to other new folks.

我对Rails比较陌生,所以我想添加一些对其他新人来说可能不太明显的东西。

Concerning the user fixture, I had to define one but leave it empty in order for this to work:

关于用户夹具,我必须定义一个,但要让它空着才能工作:

# in users.yml    
joe: {}

When using the devise sign_in helper, you can access the hash object directly in your test:

当您使用designsign_in helper时,您可以在测试中直接访问散列对象:

# a test method in some_controller_test.rb
sign_in users(:joe)

See http://guides.rubyonrails.org/testing.html#the-low-down-on-fixtures for more information on Rails fixtures.

有关Rails fixture的更多信息,请参见http://guides.rubyonrails.org/testing.html#。

#4


1  

Quoting verbatim from https://github.com/plataformatec/devise:

从https://github.com/plataformatec/devise逐字引用:

If you're using RSpec, you can put the following inside a file named spec/support/devise.rb:

如果您正在使用RSpec,您可以将以下内容放入一个名为spec/support/devise的文件中:

RSpec.configure do |config|

RSpec。配置配置| |

config.include Devise::TestHelpers, :type => :controller

配置。包括设计:::TestHelpers,:type =>:controller

end

结束

You can now use sign_in and sign_out in your RSpec tests.

现在可以在RSpec测试中使用sign_in和sign_out。

#5


0  

In addition to the code in the test_helpers.rb, I added this at the top of the controller_test and it worked for me:

除了test_helper中的代码之外。rb,我在controller_test顶部添加了这个,它对我有效:

require 'test_helper'