title: RSpec to test raise exception in rails
body: <p>I am new to RSpec. I have a method in my model user_profile.rb</p>
  
  <pre><code>def self.create_from_supplement(device, structure)
    xml = Nokogiri.parse(structure.to_s)
    user_profile = nil
    auth_type = xml.%('auth_supplement/auth_type').inner_html
  
    if 'user' == auth_type
      user_details_str = xml.%('auth_supplement/supplement_data/content').inner_html rescue nil
  
      return nil if user_details_str.blank?
  
      user_details_xml = Nokogiri.parse(user_details_str)
      user_name = user_details_xml.%('username').inner_html
  
      user_profile = UserProfile.find_or_initialize_by(name: user_name)
  
      if user_profile.save
        device.update_attributes(user_profile_id: user_profile.id)
      else
        raise "User Profile Creation Failed because of #{user_profile.errors.full_messages}"
      end
  
    end
  
    return user_profile
  end
  </code></pre>
  
  <p>I am writing a unit test case to test when user_profile.save fails, the test case will expect an exception was raised. But in my user_profiles table I have only one column :name. </p>
  
  <p>How to test the case when user_profile.save fails?  <strong>The most important problem here is I dont find any way to make this user_profile.save to fail.</strong> </p>
  
  <p>Some suggests using RSpec Stubs. How do we do that?</p>