2008-09-15 4 views
17

Bortを使って学習アプリケーションを作成しました。これは、安静認証とRSpecを含む基本アプリケーションです。私はそれを稼働させ、何かをする前にユーザーがログインすることを要求する新しいオブジェクトを追加しました(コントローラーのbefore_filter :login_required)。Rails、Restful Authentication&RSpec - 認証が必要な新しいモデルをテストする方法

Rspecのジェネレータを使用して新しいモデル/コントローラを作成しました。これらのジェネレータは、いくつかのデフォルトを作成しましたテスト。 before_filterがなければすべて通過しますが、before_filterがいったん完成すると、いくつかは失敗します。

ログインしたユーザーがいるかのように生成されたテストを実行するにはどうすればよいですか?ログインしていないマッチングのバッチが必要ですか?テストをリダイレクトしますか?私はそれがいくつかの嘲笑や治具テクニックだと考えていますが、私はRSpecを少し熟考しています。良いRSpecのチュートリアルのリンクもありがとうございます。

答えて

7

私は非常によく似た設定をしていますが、私は現在この物をテストするために使っているコードです。あなたが所有権を必要とする場合は、必要なのはログイン、または

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 

ある場合

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

:私は入れdescribe秒のそれぞれで。明らかに、ヘルパーメソッドは各ターンで(ほとんど)変化しませんが、これはほとんどの作業を行います。これは私のspec_helper.rbにあります

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 
4

私は自分自身の質問にいくつか答えを見つけました。基本的には、私はrestful_authenticationからユーザを模倣して、before_filter: login_requiredを追加したにもかかわらず、自動生成されたrspecコントローラのテストに合格する方法を理解する必要がありました。ここで

は私だけで見つかったリソースのほんの一部です:

RSpec: It Should Behave Like

rspec, restful_authentication, and login_required

using restful_authentication current_user inside controller specs

DRYing up your CRUD controller RSpecs

1

に記録されてユーザーを欺くために、私はにハッキングコントローラを@current_userに手動で設定する:

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 
7
認証をテストするが、ユーザが、私は通常、フィルタ法スタブ認証される必要がコントローラーのテストではない

:私のbefore_filterがauthenticateメソッドに設定されている

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

上記の例では、作品を:

before_filter :authenticate 

私のアプリの認証はHTTP基本認証を使用していますが、本当に他の認証メカニズムでもかまいません。

private 
def authenticate 
    authenticate_or_request_with_http_basic do |user,password| 
    user == USER_NAME && password == PASSWORD 
    end 
end 

私はテストするのがかなり簡単な方法だと思います。

+0

甘いもの! – AnkitG

関連する問題