Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8001503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:06:32+00:00 2026-06-04T16:06:32+00:00

I’m writing a gem for an API Wrapper that will be used for several

  • 0

I’m writing a gem for an API Wrapper that will be used for several applications. I don’t have control over this API but i do have a dev env where i can mess up without affecting production.

How would you write tests for this gem?

Example: consider method add_customer which takes several parameters ( name, email etc. ) with email being unique.

If i write a test that inserts a customer with email a@gmail.com. At the second time, this test will fail although my gem is properly working.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:06:34+00:00Added an answer on June 4, 2026 at 4:06 pm

    Normally you would like to clean up your db for each test you are running. We are using the DatabaseCleaner gem to do this in ruby. Basically before we run the whole suite we completely clean the database with

    DatabaseCleaner.clean_with :truncation
    

    This takes some time depending on your db. After that we use the transaction strategy to keep it faster

    DatabaseCleaner.strategy = :transaction
    

    This means that before every spec a transaction is started in the db(a save point is created) and the test runs in this transaction. After the test has finished, a rollback is issued and the next spec has a clean db again. Check out https://github.com/bmabey/database_cleaner

    If you do need to create two such records in the same spec though then you will need to create some kind of counter. Something like:

    class Counter
      def initialize
        @count = 0
      end
    
      def email
        @count += 1
        "something_cool#{@count}@whatever.com"
      end
    end
    

    And then use this counter to give you a unique e-mail every time. It might be worth the while to check out the FactoryGirl gem https://github.com/thoughtbot/factory_girl/wiki/Usage and look for sequence. If you are building a small gem though then you’d probably not want to add a lot of dependencies. Good Luck. TDD all the way!

    Not having control over all parts of the system makes for a bigger problem. In unit tests you can happily stub away all external APIs and just return the expected results. But of course this will still be passing if the API changes. Integration tests of distributed systems is kind of a complex issue and basically you would need something like system-wide transactions that will put all the parts of the system into the original state. Since you do not have that control I would just stub away.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need a function that will clean a strings' special characters. I do NOT
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.