ruby on rails - Testing a loop behaviour using Rspec -


the code:

def self.api_create(params)   user_image = userimage.new(params)   begin     user_image.uid = uniqueid.generate[0..7]   end     while not(userimage.find_all_by_uid(user_image.uid).empty?)     return user_image   end end 

unique_id primary key of userimage

how can test following test case?:

it "should create new unique id if 1 generated in use."  end 

params = ... user_image = userimage.api_create(params) userimage.find_all_by_uid(user_image.id).should be_empty 

after comment

what this

# first call returns 1, second 2 userimage.stub!(:find_all_by_uid).and_return(1,2)   # first call returns 1 method needs call uniqueid.stub!(:generate).and_return(1,3)  # let work (whatever params is) user_image = userimage.api_create(params)  # user id should 3 user_image.id.should == 3 

Comments