我刚刚实现了OmniAuth(使用Ryan Bates的Screencast http://asciicasts.com/episodes/235-omniauth-part-1)并且正在为功能编写Rspec测试,并且在测试authentifications #create action时遇到了麻烦。我对如何测试这个问题感到十分茫然 - 特别是如何对局部变量omniauth进行存根。无论我尝试什么,我都不能让任何测试工作。
采取行动的减少版本,您将如何测试在用户上调用新内容
#cut down version of the authentifications controller code I am attempting to test
def create
omniauth = request.env["omniauth.auth"]
authentification = Authentification.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
....
user = User.new
....
end
#example test
it "should create a new user" do
subject.stub_chain(:request,:env) {{"omniauth.auth" => {'provider' =>1, 'uid' => 2}}}
User.should_receive(:new)
post :create
end
答案 0 :(得分:3)
我做到了:
class SessionsController < ApplicationController
def create
@user = User.find_by_auth_hash(auth_hash)
end
def auth_hash
request.env['omniauth.auth']
end
end
describe SessionsController do
it 'should allow login' do
controller.stub!(:auth_hash).and_return({'provider' => 'twitter', 'uid' => '1234'})
get :create, :provider => 'twitter'
assigns(:user).should_not be_nil
end
end
希望有所帮助。