unit testing - how do you stub a local variable in an rspec controller test -
i've implemented omniauth (using ryan bates' screencast http://asciicasts.com/episodes/235-omniauth-part-1) , writing rspec tests functionality , have ran trouble testing authentifications#create action. i'm @ quite loss how test 1 -- in particular how stub local variable omniauth. no matter try keep can't tests work.
taking cut down version of action, how test new called on user example
#cut down version of authentifications controller code attempting 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 "should create new user" subject.stub_chain(:request,:env) {{"omniauth.auth" => {'provider' =>1, 'uid' => 2}}} user.should_receive(:new) post :create end
i did :
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 'should allow login' controller.stub!(:auth_hash).and_return({'provider' => 'twitter', 'uid' => '1234'}) :create, :provider => 'twitter' assigns(:user).should_not be_nil end end
hope helps.
Comments
Post a Comment