2010-12-09 97 views
4

我有一個可以創建會話的控制器會話。 我想從控制檯調用它,如controller.create。 這裏是動作:Rails從控制檯調用控制器動作



    def create 
    #raise request.env["omniauth.auth"].to_yaml 

    auth = request.env["omniauth.auth"] 
    user = User.find_by_provider_and_uid(auth["provider"], auth["uid"]) || User.create_with_omniauth(auth) 
    user.create_or_update_profile(auth) 
    session[:user_id] = user.id 

    if user.needs_to_create_profile? 
     redirect_to new_profile_path, :notice => "Signed in!. We just need your contact e-mail" 
    else 
     redirect_to root_url, :notice => "Signed in!" 
    end 
    end 
+1

回答http://stackoverflow.com/questions/151030/how的/複製-do -i-call-controller-view-methods-from-the-console-in-rails/1161163#1161163 – 2010-12-09 17:04:17

+0

似乎是以下的副本:http://stackoverflow.com/questions/151030/how-do- I-呼叫控制器 - 視圖 - 方法-從 - - 控制檯 - in-rails – Brian 2010-12-09 17:04:48

回答

12

從控制檯:


    include ActionController::TestProcess 
    @request = ActionController::TestRequest.new 
    @response = ActionController::TestResponse.new 
    @controller = SomeController.new 

    @request.env["omniauth.auth"] = {'provider' => "twitter", 'uid' => "1234", 'user_info' => {'name' => "foo"}} 

    app.get "/signup" etc 

從RSpec的:



it "should allow login" do 
    request.env["omniauth.auth"] = {'provider' => "twitter", 'uid' => "1234", 'user_info' => {'name' => "foo"}} 
    post :create 
    puts @current_user.name 
    assigns(@current_user).should_not be_nil 
    end