1

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 is the primary key of UserImage

How can test the following test case?:

it "should create a new unique id if the one generated is already in use." do

end

1 Answer 1

1
params = ...
user_image = UserImage.api_create(params)
UserImage.find_all_by_uid(user_image.id).should be_empty

after comment

what about this

# first call returns 1, second 2
UserImage.stub!(:find_all_by_uid).and_return(1,2) 

# first call returns 1 so that the method needs another call
UniqueId.stub!(:generate).and_return(1,3)

# let it work (whatever params is)
user_image = UserImage.api_create(params)

# the user id should be 3
user_image.id.should == 3
Sign up to request clarification or add additional context in comments.

1 Comment

I want to test the following, if UniqueId.generate[0..7] retrives a uid which is used by some other record, call UniqueId.generate[0..7] again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.