Richard
Richard

Reputation: 1579

rspec not saving to database

I'm trying to test the login method below, but it does not seem to be saving the data_provider_user to the database. I've checked using debugger and it works perfectly until it leaves the method and goes back to the rspec code, as shown below. I've also checked to see if the object is valid after it save using .valid? and it is so I'm a bit lost!

def login
 require 'TwitterOauth'
 @data_provider_user = DataProviderUser.find(params[:id])

 if @data_provider_user.twitter?

  @request_token = TwitterOauth.request_url

  @data_provider_user.access_token = @request_token.token
  @data_provider_user.oauth_token_secret = @request_token.secret

  if @data_provider_user.save
    debugger
    redirect_to @request_token.authorize_url              
  end      

 end
end

Test:

it "should update the tokens" do
    require 'TwitterOauth'
    TwitterOauth.stub(:request_url).and_return(@token)
    debugger
    get :login, {id: @data_provider_user.id}
    debugger
    @data_provider_user.access_token.should_not eq(nil)
    @data_provider_user.oauth_token_secret.should_not eq(nil)
  end

Debugger output:

#<DataProviderUser id: 84, user_id: 63, data_provider_id: 63, username: nil, password: nil, created_at: "2013-02-19 15:21:59", updated_at: "2013-02-19 15:21:59", access_token: nil, update_frequency: nil, oauth_token_secret: nil>

#<DataProviderUser id: 84, user_id: 63, data_provider_id: 63, username: nil, password: nil, created_at: "2013-02-19 15:21:59", updated_at: "2013-02-19 15:22:12", access_token: "186553918-sEAEO2fcvtyO1x99eH4Q4XwVcOYatODCQ5f1TwqD", update_frequency: nil, oauth_token_secret: "gLw2PtUyTZfxIan1gJBnbP7icboXbi98KlUoOn7ycVs">

#<DataProviderUser id: 84, user_id: 63, data_provider_id: 63, username: nil, password: nil, created_at: "2013-02-19 15:21:59", updated_at: "2013-02-19 15:21:59", access_token: nil, update_frequency: nil, oauth_token_secret: nil>

Any help would be greatly appreciated!!

------ UPDATE -------

There was actually nothing wrong with the code, the issue was that rspec was not reloading it in time. A way to get around this was to use:

@data_provider_user.reload 

before hand which refreshes the object, updating the values accordingly.

Upvotes: 1

Views: 4161

Answers (3)

mpz
mpz

Reputation: 1850

In spec/rails_helper.rb

Set false to use_transactional_fixtures:

# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = false

Upvotes: 1

Richard
Richard

Reputation: 1579

There was actually nothing wrong with the code, the issue was that rspec was not reloading it in time. A way to get around this was to use:

@data_provider_user.reload 

before hand which refreshes the object, updating the values accordingly.

Upvotes: 4

ckim
ckim

Reputation: 1004

Are you sure you're looking at the right database? When you're doing testing it's probably going to myapp_test (myapp being whatever you called your app). You may be looking at your myapp_development database.

If you're accessing the database via rails db chances are you're going into your development database. You can check with select database(); if you're using MySQL. If you want to check your test database, then use RAILS_ENV=test rails db. Again, you can use the select statement to verify you're in the right database.

Upvotes: 0

Related Questions