title: Passing variable between multiple contexts with Rspec
body: <p>I'm writing some tests using <code>FactoryGirl</code> and <code>Rspec</code>.</p>
  
  <p><strong>spec/factories/students.rb</strong>:</p>
  
  <pre><code>FactoryGirl.define do
    factory :student do
    end
  
    factory :student_with_profile_and_identity, class: 'Student' do
      after(:create) do |student|
        create(:profile, profileable: student)
        create(:student_identity, student: student)
      end
    end
  end
  </code></pre>
  
  <p><strong>spec/factories/profiles.rb</strong>:</p>
  
  <pre><code>FactoryGirl.define do
    factory :profile do
      birthday { Faker::Date.birthday(15, 150) }
      sequence(:email) { |i| "profile_#{i}@email.com" }
      first_name { Faker::Name.first_name }
      last_name { Faker::Name.first_name }
      password { Faker::Internet.password(6, 72, true, true) }
    end
  end
  </code></pre>
  
  <p><strong>spec/factories/student_identities.rb</strong>:</p>
  
  <pre><code>FactoryGirl.define do
    factory :student_identity do
      provider { ['facebook.com', 'google.com', 'twitter.com'].sample }
      uid { Faker::Number.number(10) }
    end
  end
  </code></pre>
  
  <p><strong>spec/requests/authorizations_spec.rb</strong>:</p>
  
  <pre><code>require 'rails_helper'
  
  RSpec.describe 'Authorizations', type: :request do
    describe 'POST /v1/authorizations/sign_in' do
      let!(:student) { create(:student_with_profile_and_identity) }
  
      context 'when the request is valid' do
        subject do
          post '/v1/authorizations/sign_in',
               params: credentials
        end
  
        context "user signs up via social network" do
          let(:credentials) do
            {
              authorization: {
                student: {
                  profile_attributes: {
                    email: student.profile.email
                  },
                  student_identities_attributes: {
                    provider: student.student_identities[0].provider,
                    uid: student.student_identities[0].uid
                  }
                }
              }
            }
          end
  
          it 'returns an authentication token' do
            subject
            p "1 student.profile.inspect #{student.profile.inspect}"
            expect(json['token']).to(be_present)
          end
        end
  
        context 'when the user has already an account' do
          let(:credentials) do
            {
              authorization: {
                email: student.profile.email,
                password: student.profile.password
              }
            }
          end
  
          it 'returns an authentication token' do
            p "2 student.profile.inspect #{student.profile.inspect}"
            subject
            expect(json['token']).to(be_present)
          end
        end
      end
    end
  end
  </code></pre>
  
  <p>Almost all tests are passing... the problem is that:</p>
  
  <p>It's creating a new <em>student</em> in every context. I'd expect the <code>let!(:student) { ... }</code> to be something like <em>"singleton"</em>, in other words, once it's created/defined here <code>let!(:student) { create(:student_with_profile_and_identity) }</code> it won't be called anymore.</p>
  
  <p>Ex: the logs are like this:</p>
  
  <pre><code>"1 student.profile.inspect #&lt;Profile id: 1, email: \"profile_1@email.com\", profileable_type: \"Student\", profileable_id: 1&gt;"
  
  "2 student.profile.inspect #&lt;Profile id: 2, email: \"profile_2@email.com\", profileable_type: \"Student\", profileable_id: 2&gt;"
  </code></pre>
  
  <p>While I'd expect the instances to be the same.</p>
  
  <p>Am I missing something?</p>