RSpec-2 和设计

发布于 2024-09-26 14:04:24 字数 851 浏览 2 评论 0原文

我创建了一个定制的设备注册控制器,我想用 rspec 来测试它。

我已经尝试过一个非常简单的测试:

it "creates a new parent" do
  Parent.should receive(:new)
  post :create
end

但我得到了这个例外:

Failures:
  1) Parent::RegistrationsController POST create creates a new parent
     Failure/Error: post :create, { :commit => "Daftar",
     uncaught throw `warden'
     # /home/starqle/.rvm/gems/ree-1.8.7-2010.02/gems/devise-1.1.3/lib/devise/hooks/timeoutable.rb:16:in `throw'
     # /home/starqle/.rvm/gems/ree-1.8.7-2010.02/gems/devise-1.1.3/lib/devise/hooks/timeoutable.rb:16

我已经将这一行放入我的测试中:

describe Parent::RegistrationsController do
  include Devise::TestHelpers
end

我也已经将这一行放入:

request.env["devise_mapping"] = Devise.mappings[:parent]

有人有解决这个问题的想法吗?

i create a customized devise registration controller and i want to test it with rspec.

I've tried it with a very simple test :

it "creates a new parent" do
  Parent.should receive(:new)
  post :create
end

but i get this exception:

Failures:
  1) Parent::RegistrationsController POST create creates a new parent
     Failure/Error: post :create, { :commit => "Daftar",
     uncaught throw `warden'
     # /home/starqle/.rvm/gems/ree-1.8.7-2010.02/gems/devise-1.1.3/lib/devise/hooks/timeoutable.rb:16:in `throw'
     # /home/starqle/.rvm/gems/ree-1.8.7-2010.02/gems/devise-1.1.3/lib/devise/hooks/timeoutable.rb:16

I already put this line within my test:

describe Parent::RegistrationsController do
  include Devise::TestHelpers
end

I also already put this line:

request.env["devise_mapping"] = Devise.mappings[:parent]

anybody have ideas to solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柳若烟 2024-10-03 14:04:24

我之前的回答有点令人困惑。对不起。

更新的答案:根本原因是用户在“登录”之前未“确认”。

@user.confirm! 
sign_in @user

那么一切都很好。

My previous answer is a little confusing. sorry.

Updated answer: root cause is user is not "confirmed" before "sign in".

@user.confirm! 
sign_in @user

then everything is fine.

予囚 2024-10-03 14:04:24

我对红宝石比较陌生。
我正在将 Rails 3 与 devise 和 Factory Girl 一起使用。

我正在寻找如何对 rspec 的用户进行身份验证。

我被困在 before_filter:authenticate_user!在控制器中。

终于找到解决方案了(感谢沉思伟)
我正在做的是

  1. 将 TestHelpers 包含在 spec/spec_helper.rb

2 中。

require 'spec_helper'

describe StudentsController do
  before(:each) do
    @user = Factory.create(:user)  #:user from factory girl with admin privilages
    @request.env['devise.mapping'] = :user
    @user.confirm!
    sign_in @user
  end

  it "can get index of student" do
    get :index
    response.should be_suclogin_as @user
  end

  it "can create student" do
    #in student model : validates :name, :presence=> true 
    post :create, :student => {name => "student1" } 
    answer = Student.find_by_name("student1")
    answer.name.should == "student1"
  end
end

I am fresher in ruby.
I am using rails 3 with devise and factory girl.

I was searching for how to authenticate user for rspec.

I was stucked at before_filter: authenticate_user! in controller.

Finally I got solution (thanks to Siwei Shen)
What I am doing is

  1. include TestHelpers in spec/spec_helper.rb

2.

require 'spec_helper'

describe StudentsController do
  before(:each) do
    @user = Factory.create(:user)  #:user from factory girl with admin privilages
    @request.env['devise.mapping'] = :user
    @user.confirm!
    sign_in @user
  end

  it "can get index of student" do
    get :index
    response.should be_suclogin_as @user
  end

  it "can create student" do
    #in student model : validates :name, :presence=> true 
    post :create, :student => {name => "student1" } 
    answer = Student.find_by_name("student1")
    answer.name.should == "student1"
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文