简体   繁体   English

Rspec2控制器的设计与测试

[英]Rspec2 controller testing with devise

I'm currently new to RSpec and trying to implement some Controller testing with RSpec 我目前是RSpec的新手,并尝试使用RSpec进行一些控制器测试

In my Rails app, I'm using Devise as my authentication system. 在我的Rails应用程序中,我使用Devise作为身份验证系统。 My question is, When we test a controller which uses some authentication system (in my case Devise), what is the standard practice? 我的问题是,当我们测试使用某种身份验证系统的控制器(在我的示例中为Devise)时,标准做法是什么?

Is it 是吗

1 - to skip the authentication 1-跳过身份验证

or 要么

2 - to authenticate the controller 2-验证控制器

as per the question, following is my controller 根据问题,以下是我的控制器

require File.dirname(__FILE__) + '/../spec_helper'

describe ProjectsController do
  include Devise::TestHelpers

  p "starting..."

  before(:each) do
    p "in before method"
    @request.env["devise.mapping"] = Devise.mappings[:user]
    sign_in Factory.create(:user)
  end

  it "should create a project" do
   p "should create a project"
  end

  after(:each) do
    @user.destroy unless @user.nil?
  end
end 

I can only see ' starting ', But why its not going to " in before method " and " should create a project " 我只能看到“ starting ”,但是为什么它不去“ in before method ”和“ should create a project

I'm using Rspec2 and Rails2 on Ubuntu. 我在Ubuntu上使用Rspec2和Rails2。

Check this: Stubbing Devise in rSpec and Rails3 . 检查一下: 在rSpec和Rails3中存根Devise

Standard practice is not skipping authentication, but effectively making sure that a correct user is logged in (for devise). 标准做法不是跳过身份验证,而是有效地确保登录了正确的用户(用于设计)。

Referring to your code: have you tried to create some real test? 参考您的代码:您是否尝试过创建一些真实的测试? Eg something as simple as 例如简单的事情

it "gets index" do
  get :index
  response.status.should be == 200
end

I am not sure why you are not seeing the print-statements. 我不确定为什么您看不到打印语句。 Either rspec skips the empty step (there is no real code), or because something else went wrong. rspec跳过了空步骤(没有实际代码),或者是因为其他问题出了错。 But honestly, I am not even sure if using p inside rspec works. 但老实说,我什至不确定在rspec中使用p是否有效。

A tool like rubymine allows you to easily debug your specs if you want to step into it (which imho is a better approach then the scattered p statements). 像rubymine这样的工具可以让您轻松调试规范(如果想要加入它,imho是比分散的p语句更好的方法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM