关于测试:Rails,Restful Authentication和RSpec-如何测试需要认证的新模型

关于测试:Rails,Restful Authentication和RSpec-如何测试需要认证的新模型

Rails, Restful Authentication & RSpec - How to test new models that require authentication

我使用Bort创建了一个学习应用程序,该应用程序是包括Restful Authentication和RSpec的基本应用程序。 我已经启动并运行它,并添加了一个新对象,该对象要求用户必须先登录才能执行任何操作(控制器中的before_filter :login_required)。 [编辑:我还应该提到新类的用户has_many,只有该用户才能看到它。]

我使用Rspec的生成器创建了新的模型/控制器,这些生成器创建了许多默认测试。 如果没有before_filter,它们都将通过,但是一旦before_filter到位,就会有几次失败,这是可以预料的。

如何让生成的测试像有/没有登录用户一样运行? 我是否需要一整批未登录的匹配项-重定向测试? 我认为这是一种模拟或夹具技术,但是我对RSpec还是陌生的。 良好的RSpec教程链接也将不胜感激。


当不测试身份验证而是测试需要用户身份验证的控制器时,我通常会添加过滤方法:

1
2
3
before(:each) do
  controller.stub!(:authenticate).and_return(true)
end

上面的示例在我的before_filter设置为authenticate方法的情况下起作用:

1
before_filter :authenticate

并且我的应用中的身份验证使用HTTP基本身份验证,但实际上可以是任何其他身份验证机制。

1
2
3
4
5
6
private
def authenticate
  authenticate_or_request_with_http_basic do |user,password|
    user == USER_NAME && password == PASSWORD
  end
end

我认为这是一种非常简单的测试方法。


我有一个非常相似的设置,下面是我目前用来测试这些东西的代码。在每个describe中,我输入:

1
2
it_should_behave_like"login-required object"
def attempt_access; do_post; end

如果您只需要登录,或者

1
2
3
4
5
6
it_should_behave_like"ownership-required object"
def login_as_object_owner; login_as @product.user; end
def attempt_access; do_put; end
def successful_ownership_access
  response.should redirect_to(product_url(@product))
end

如果您需要所有权。显然,每转一圈,辅助方法都会发生变化(很小),但这将为您完成大部分工作。这是在我的spec_helper.rb中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
shared_examples_for"login-required object" do
  it"should not be able to access this without logging in" do
    attempt_access

    response.should_not be_success
    respond_to do |format|
      format.html { redirect_to(login_url) }
      format.xml { response.status_code.should == 401 }
    end
  end
end

shared_examples_for"ownership-required object" do
  it_should_behave_like"login-required object"

  it"should not be able to access this without owning it" do
    attempt_access

    response.should_not be_success
    respond_to do |format|
      format.html { response.should be_redirect }
      format.xml { response.status_code.should == 401 }
    end
  end

  it"should be able to access this if you own it" do
    login_as_object_owner
    attempt_access

    if respond_to?(:successful_ownership_access)
      successful_ownership_access
    else
      response.should be_success
    end
  end
end

我找到了自己问题的一些答案。基本上,我需要了解如何从restful_authentication中模拟用户,以便即使我添加了before_filter: login_required,自动生成的rspec控制器测试也可以通过。

以下是一些我刚刚找到的资源:

RSpec:应该像

rspec,restful_authentication和login_required

在控制器规格中使用restful_authentication current_user

使CRUD控制器RSpec干燥


为了模拟正在登录的用户,我侵入控制器以手动设置@current_user

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
module AuthHelper
  protected

  def login_as(model, id_or_attributes = {})
    attributes = id_or_attributes.is_a?(Fixnum) ? {:id => id} : id_or_attributes
    @current_user = stub_model(model, attributes)
    target = controller rescue template
    target.instance_variable_set '@current_user', @current_user

    if block_given?
      yield
      target.instance_variable_set '@current_user', nil
    end
    return @current_user
  end

  def login_as_user(id_or_attributes = {}, &block)
    login_as(User, id_or_attributes, &block)
  end
end


推荐阅读

    测试linux常用命令?

    测试linux常用命令?,工作,地址,系统,信息,命令,目录,情况,标准,功能,控制台,l

    linux命令测试客户端?

    linux命令测试客户端?,地址,系统,网络,工具,工作,分析,环境,命令,下行,资料,l

    linux测试url命令?

    linux测试url命令?,系统,网络,地址,基础,分析,工作,命令,实战,管理,认证,如何

    测试linux一般命令?

    测试linux一般命令?,工作,系统,地址,信息,命令,管理,数据,标准,单位,目录,lin

    linux命令测试流量?

    linux命令测试流量?,工具,系统,实时,网址,网络,情况,地址,状态,时间,电脑,查

    测试用的linux命令?

    测试用的linux命令?,地址,系统,工作,信息,网络,管理,基础,命令,电脑,目录,软

    linux性能测试命令?

    linux性能测试命令?,数据,系统,工具,标准,设备,地址,情况,基础,网络,环境,如

    linux并发测试命令?

    linux并发测试命令?,网络,地址,系统,工作,服务,网址,检测,软件,标准,信息,lin

    linux命令行网页认证?

    linux命令行网页认证?,网络,系统,环境,地址,传播,电脑,认证,密码,发行,命令,l

    linux操作命令测试?

    linux操作命令测试?,网络,系统,地址,情况,工作,命令,状态,分析,检测,标准,Lin

    手机上测试linux命令?

    手机上测试linux命令?,地址,工作,电脑,软件,手机,系统,名字,网络,信息,平台,l

    学习linux命令心得?

    学习linux命令心得?,基础,基础知识,报告,系统,管理,时间,社会,数字,技术,命

    linux命令和应用程序?

    linux命令和应用程序?,软件,系统,环境,管理,基础,情况,位置,电脑,工具,中心,

    linux系统测试命令?

    linux系统测试命令?,系统,工作,地址,标准,信息,命令,数据,单位,软件,目录,软

    linux终端命令测试题?

    linux终端命令测试题?,系统,第一,定期,工作,密码,连续,环境,认证,工具,设备,

    学习python的三个阶段

    学习python的三个阶段,项目,代码,初级,公司,工程,时间,员工,数据,资料,第一

    Python在测试中的应用

    Python在测试中的应用,平台,工具,代码,人员,工作,系统,软件,测试,标准,官网