ruby on rails - How to test a class method in rspec that returns a model instance -


i'm trying test user model's class method #registered_but_not_logged_in(email), grabs first user matches email has confirmed_at entry has never logged in (which i'm counting sign_in_count). i'm using rspec factorygirl, plus shoulda-matchers v2.8.

here's ruby:

def self.registered_but_not_logged_in email   self.with_email( email ).confirmed.never_signed_in.first end  

i've tested in rails console , know works expected it's not logic problem on end, i'm thinking i'm doing wrong in test:

describe user   # create @user    describe ".registered_but_not_logged_in"     "returns user matches provided email confirmed, has not yet signed in"       @user.confirmed_at = 2.days.ago       @user.email = "fisterroboto5893@mailinator.com"        result = described_class.registered_but_not_logged_in("fisterroboto5893@mailinator.com")       expect(result).to be_instance_of(user)     end   end 

in example, result nil. know case of @user existing outside database while method actively checking db, don't know how handle while using rspec/factorygirl. appreciated!

so i'm not 100% sure why i'm doing working, here's solution stumbled across of @nort , 1 of coworkers:

it "returns user matches provided email confirmed, has not yet signed in"   @user.confirmed_at = 2.days.ago   @user.email = "fisterroboto5893@mailinator.com"   @user.sign_in_count = 0   @user.save!   expect(user.registered_but_not_logged("fisterroboto5893@mailinator.com")).to be_instance_of(user) end 

what believe happening save! saving @user test database, otherwise unpopulated develop against different db. issue of course being can't test data doesn't exist.

as bonus, note expect().to... preferred convention rpsec. also, described_class believe totally work fine, preferring explicitness right since i'm still learning stuff.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -