title: Using RSpec with Factory Girl and get a can't define singleton error
body: <p>I'm using Factory Girl to generate test date in order to mock a connection to an ldap server (because I don't want the application to make a real ldap connection each time in order to successfully run my tests).</p>
  <p>I want to mock this connection and confirm that this occurred successfully.</p>
  <p>My factory girl data is:</p>
  <p><strong>/factories/connections.rb</strong></p>
  <pre><code>FactoryGirl.define do
    factory :connection do
      host &quot;just-ln1-wdc03.company.qube&quot;
      port 389
      base &quot;DC=company,DC=qube&quot;
      username &quot;cat@company.qube&quot;
      password &quot;cats123&quot;
    end
  end
  </code></pre>
  <p>and my Rspec test is:</p>
  <p><strong>authenticate_spec.rb</strong></p>
  <pre><code>require 'spec_helper'
  
  describe &quot;#is_authenticated_by_ldap&quot; do 
    let(:connection) {FactoryGirl.build(:connection)}
  
    it &quot;queries ldap server and returns success&quot; do
      allow(:connection).to receive(:is_authenticated_by_ldap).with(&quot;cat@company.qube&quot;, &quot;cats123&quot;).and_return(true)
    end
  end
  </code></pre>
  <p><strong>spec_helper.rb</strong></p>
  <pre><code>require 'factory_girl'
  require_relative './factories/connections.rb'
  
  RSpec.configure do|config|
  ...
  </code></pre>
  <p><strong>authenticate.rb</strong></p>
  <pre><code>def is_authenticated_by_ldap(credentials)
    full_username = credentials[0] + EXTENSION #EXTENSION = &quot;company.com&quot;
    ldap = Net::LDAP.new  :host =&gt; &quot;just-ln1-wdc03.company.qube&quot;, # your LDAP host name or IP goes here,
                          :port =&gt; 389, # your LDAP host port goes here,
                          # :encryption =&gt; :simple_tls,
                          :base =&gt; &quot;DC=company,DC=qube&quot;, # the base of your AD tree goes here,
                          :auth =&gt; {
                            :method =&gt; :simple,
                            :username =&gt; full_username, # a user w/sufficient privileges to read from AD goes here,
                            :password =&gt; credentials[1] # the user's password goes here
                          }   
          
    is_authorized = ldap.bind
    return is_authorized
  end
  </code></pre>
  <p>parameters passed to the <code>is_authenticated_by_ldap</code> method are essentially username and password effectively credentials[0] and credentials[1] respectively.</p>
  <p>When I run this <code>rspec spec/authenticate_spec.rb</code>, I receive the following error:</p>
  <pre><code>Authenticate
  ←[32m  authenticates a user on qube server←[0m
    #is_authenticated_by_ldap
  ←[31m    queries ldap server and returns success (FAILED - 1)←[0m
  
  Failures:
    1) Authenticate#is_authenticated_by_ldap queries ldap server and returns success
       ←[31mFailure/Error: allow(:connection).to receive(:is_authenticated_by_ldap).with(&quot;cats@company.qube&quot;, &quot;cats123&quot;).and_return(true)←[0m
       ←[31mTypeError:←[0m
       ←[31m  can't define singleton←[0m
  </code></pre>
  <h1>UPDATE</h1>
  <p>I believe I have answered my own question and have posted an explanation here as someone else may have this problem and seek a solution like I did:</p>
  <p>RSpec 3.0 has a feature called an instance double which takes a class name or object as it's first argument and then verifies that any methods being stubbed would be present and execute on an instance of that class.</p>
  <p>So in my case because I had an Authenticate module which handled my authentication feature, I created a dummy class in my Rspec test, which also included the said module.(see code below)</p>
  <h1>authenticate_spec.rb</h1>
  <p>require 'spec_helper'</p>
  <pre><code>class Dummy
      include Authenticate
  end 
  
  describe Authenticate do
  
          let(:dummy_class) {Dummy.new.authenticate}
          let(:credentials) {FactoryGirl.build_stubbed(:credentials)}
  
              it &quot;authenticates a user on qube server&quot; do 
  
                  expect(:dummy_class).to be
  
              end 
  
  
          describe &quot;#is_authenticated_by_ldap&quot; do 
  
              it &quot;queries ldap server and returns successful connection&quot; do
  
                  dummy = instance_double(&quot;Dummy&quot;)
                  
                  allow(dummy).to receive(:is_authenticated_by_ldap).with(:credentials).and_return(true)
              
                  expect(dummy.is_authenticated_by_ldap(:credentials)).to be(true)
  
              end
  
      end
  
  
  end
  
  #spec/factories/credentials.rb
  
  FactoryGirl.define do
    factory :credentials do
      username &quot;cats@company.com&quot;
      password &quot;cats123&quot;
    end
  end 
  </code></pre>
  <p>and defined test data using factory_girl (username and password) to pass as a parameter of the is_authenticated_by_ldap method.(this method confirms a successful connection to the ldap server or not). Using RSpec 3.0 syntax, if you use stubs/mocks, the allow method with a receive matcher, tells the fake object to return a value in response to a given message/method. In my case, I would expect the test to pass (successful ldap connection) or fail (unsuccessful ldap connection). To confirm that my mock worked correctly and wasn't a false positive, test data defined by factory girl gem(and not existing on the ldap server) was used and the test passed. I also commented out the source code for the method and reran the test and it failed as expected.</p>