Ruby on Rails的设计与验证功能测试 [英] Ruby on Rails functional test with Devise authentication
问题描述
我在寻找一个奇怪的问题的解决方案。我有一个控制器,需要验证(与色器件宝石)。我加入了设计TestHelpers但我无法得到它的工作。
I'm searching for a solution for a weird problem. I have a controller, that needs authentication (with the devise gem). I added the Devise TestHelpers but i can't get it working.
require 'test_helper'
class KeysControllerTest < ActionController::TestCase
include Devise::TestHelpers
fixtures :keys
def setup
@user = User.create!(
:email => 'testuser@demomailtest.com',
:password => 'MyTestingPassword',
:password_confirmation => 'MyTestingPassword'
)
sign_in @user
@key = keys(:one)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:keys)
end
test "should get new" do
get :new
assert_response :success
end
test "should create key" do
assert_difference('Key.count') do
post :create, :key => @key.attributes
end
assert_redirected_to key_path(assigns(:key))
end
test "should destroy key" do
assert_difference('Key.count', -1) do
delete :destroy, :id => @key.to_param
end
assert_redirected_to keys_path
end
结束
和我得到以下输出在我的耙测试窗口:
And i get the following output in my "rake test" window:
29) Failure:
test_should_create_key(KeysControllerTest) [/test/functional/keys_controller_test.rb:29]:
"Key.count" didn't change by 1.
<3> expected but was
<2>.
30) Failure:
test_should_destroy_key(KeysControllerTest) [/test/functional/keys_controller_test.rb:37]:
"Key.count" didn't change by -1.
<1> expected but was
<2>.
31) Failure:
test_should_get_index(KeysControllerTest) [/test/functional/keys_controller_test.rb:19]:
Expected response to be a <:success>, but was <302>
32) Failure:
test_should_get_new(KeysControllerTest) [/test/functional/keys_controller_test.rb:25]:
Expected response to be a <:success>, but was <302>
谁能告诉我,为什么色器件不鉴定?我使用的是完全相同的程序的AdminController和它的作品完美。
Can someone tell my, why devise doesn't authenticate? I'm using the exact same procedure for an AdminController and it works perfect.
推荐答案
您使用与设计可证实?在这种情况下,创造是不够的,你需要确认 @ user.confirm用户!
Are you using Devise with confirmable? In this case, create is not enough and you need to confirm the user with @user.confirm!
二,为什么你在创建功能测试用户?声明你的用户在这样的固定装置(如果需要确认confirmed_at只):
Second, why do you create the user in the functional test? Declare your users in the fixture like this (confirmed_at if you require confirmation only):
测试/夹具/ users.yml里:
test/fixtures/users.yml:
user1:
id: 1
email: user1@test.eu
encrypted_password: abcdef1
password_salt: efvfvffdv
confirmed_at: <%= Time.now %>
和与他们签在你的功能测试:
and sign them in in your functional tests with:
sign_in users(:user1)
编辑:我刚才看到,在我的应用程序的设计,Testhelpers在测试/测试helpers.rb声明,我不知道这是否有差别,也许你也想尝试:
I just saw, that in my app the Devise-Testhelpers are declared in test/test-helpers.rb and I don't know if this makes a difference, maybe you want to try:
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActionController::TestCase
include Devise::TestHelpers
end
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
#
# Note: You'll currently still have to declare fixtures explicitly in integration tests
# -- they do not yet inherit this setting
fixtures :all
# Add more helper methods to be used by all tests here...
end
这篇关于Ruby on Rails的设计与验证功能测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!