ruby 2.6.6
rails 4.2.11
rspec-rails 3.7.2
I'm having trouble setting up a test on a SessionsController to show that
after a User is created with omniauth, an authorization is then created
that has all of its correct values.
I hope I'm not giving too much context here, but if anyone is willing to
look at this, I don't want to waste their time by not having all the
context (and there is a lot of it!)
My students are learning Agile Development and are using *Cucumber* for
their Acceptance testing and *RSpec* for their Unit testing. Many of them
are asking me about this or similar problems they are having with tests
involving proxy objects created via associations, and I honestly don't have
an answer for them. I'm scouring all of my books and the internet to find
a solution.
code in controller:
* user = User.create_with_omniauth(auth_hash['info'])*
* auth = user.authorizations.create_with_omniauth(auth_hash)*
* private*
* def auth_hash*
* # ensures availability but only retrieved once per cycle*
* @auth_hash ||= request.env['omniauth.auth']*
* end*
schema:
*ActiveRecord::Schema.define(version: 20210414024936) do*
* create_table "authorizations", force: :cascade do |t|*
* t.string "provider"*
* t.string "uid"*
* t.integer "user_id"*
* t.datetime "created_at", null: false*
* t.datetime "updated_at", null: false*
* end*
* add_index "authorizations", ["user_id"], name:
"index_authorizations_on_user_id"*
* create_table "users", force: :cascade do |t|*
* t.string "name"*
* t.string "email"*
* t.datetime "created_at", null: false*
* t.datetime "updated_at", null: false*
* end*
*end*
code in models:
*class User < ActiveRecord::Base*
* has_many :authorizations*
* validates :name, :email, :presence => true*
* # save new user info*
* def self.create_with_omniauth info*
* create!(name: info['name'], email: info['email'])*
* end*
*end*
*class Authorization < ActiveRecord::Base*
* belongs_to :user*
* validates :provider, :uid, :presence => true*
* validates_uniqueness_of :uid, scope: :provider*
* # create new authorization*
* def self.create_with_omniauth auth*
* create!(uid: auth['uid'], provider: auth['provider'])*
* end*
*end*
spec_helper.rb:
*require 'omniauth'*
*OmniAuth.config.test_mode = true*
*omniauth_hash = { 'provider' => 'github',*
* 'uid' => "12345",*
* 'info' => {*
* 'name' => "SUNY Tester",*
* 'email' =>"[email protected]",*
* }*
*}*
*OmniAuth.config.add_mock(:github, omniauth_hash)*
rails_helper.rb
*Rails.application.env_config["omniauth.auth"] =
OmniAuth.config.mock_auth[:github] *
sessions_controller_spec.rb:
*require 'rails_helper'*
*RSpec.describe SessionsController, type: :controller do*
* describe "GET #create" do*
* context 'register with github' do*
* describe 'When signing up for first time' do*
* let(:user1) {instance_double('User', name: 'SUNY Tester', email:
'[email protected]')} *
* let(:authorization1) {instance_double('Authorization', provider:
'github', uid: '12345', user_id: '1')} *
* it "creates a User" do*
* expect(User).to
receive(:create_with_omniauth).with(OmniAuth.config.mock_auth[:github]['info']).and_return(user1)*
* post :create, provider: :github *
* end*
* # none of the following work, just a couple examples of what I've
tried *
* it "creates an Authorization" do*
* #allow(user1).to
receive(:authorizations.create_with_omniauth).with(OmniAuth.config.mock_auth[:github]['info']).and_return(user1)*
* # expect(authorization1).to be_a_new(Authorization)*
* # expect(user1.authorizations).to
receive(:create_with_omniauth).with(OmniAuth.config.mock_auth[:github]).and_return(authorization1)*
* post :create, provider: :github*
* end*
The error messages are always something along the lines of:
*SessionsController GET #create register with github When signing up for
first time creates a User*
* Failure/Error: user.authorizations.create_with_omniauth(auth_hash)*
* #<InstanceDouble(User) (anonymous)> received unexpected message
:authorizations with (no args)*
If I try to create the authorization directly instead of with the proxy
object, the user_id doesn't get set.
No matter how I go about it, I can't seem to find a way to mock a proxy
object (the instance of user that is just created* .authorizations )*
Ironically, All my code as well as the students' code is working, but I
can't seem to find a proper way to test it.
--
You received this message because you are subscribed to the Google Groups
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/rspec/6cea440c-85b1-4aba-856c-3a518e421835n%40googlegroups.com.