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 675803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:51:30+00:00 2026-05-14T00:51:30+00:00

I have a number of models that store words as a string with each

  • 0

I have a number of models that store words as a string with each words separated by a space. I have defined model methods to delete and add words to the string and the size or number of words is updated each time accordingly. There is an error somewhere because the size sometimes comes out to be negative. I’m wondering, what is the best way to test a situation like this in rails. I would ideally like to write a test that allows me to add a number of words and remove them while verifying the correct value of size each time. Thanks.

  • 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-05-14T00:51:31+00:00Added an answer on May 14, 2026 at 12:51 am

    I assume your model looks something like this? I have omitted some code for simplicity since your question isn’t about how to implement the word management piece but on how to test it.

    class A
      def add_word(word)
        # Add the word to the string here
      end
    
      def delete_word(word)
        # Find the word in the string and delete it
      end
    
      def count_words
        # Probably split on " " and then call length on the resulting array
      end
    end
    

    Now you can just write a simple unit test.

    require File.dirname(__FILE__) + '/../../../test_helper'
    
    class EpisodeTest < ActiveSupport::TestCase 
      def test_word_count
        a = new A()
    
        assert_equal(0, a.count_words)
    
        a.add_word("foo")
        assert_equal(1, a.count_words)
        assert_equal("foo", words)
    
        a.add_word("bar")
        assert_equal(2, a.count_words)
        assert_equal("foo bar", words)
    
        a.delete_word("foo")
        assert_equal(1, a.count_words)
        assert_equal("bar", words)
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a core data model, using the database store, that contains entities with
I have 3 model classes: class Team(models.Model): name = models.CharField(max_length=100, default=, blank=True, null=True) number
I have a Django model with a large number of fields and 20000+ table
I have a number of code value tables that contain a code and a
I have a number of tables that use the trigger/sequence column to simulate auto_increment
I have a number of RGBA pixels, each of them has an alpha component.
I have a model that requires loading external data from an auxiliary source. A
I have a couple of models that are both password centric models. They don't
Say I have the following model: class Person(models.Model): street_address = models.CharField(max_length=50, blank=True) suburb =
I have to store messages that my web app fetch from Twitter into a

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.