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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:24:26+00:00 2026-05-20T23:24:26+00:00

I have a vote model, which has a class method called score . Basically,

  • 0

I have a vote model, which has a class method called score. Basically, I created a mathematical equation in a spreadsheet, and am attempting to reproduce this in ruby. However, my first go isn’t working, so I really need to start adding a ton more tests.

The way I’d like to go about testing this is to take a stack of input and output values from my spreadsheet and test them each. So basically, the tests could be construed to look like this:

 inputs = [a,b,c] ... score.should == x
 inputs = [a,b,c,d] ... score.should == y
 inputs = [c,d] .... score.should == z

However, the shortest way I’ve actually found to write this in RSpec is giving an example for each case, approximately like this (simplified example, but should give you the idea):

it "should have a score of X" do
  test_object = Votable.new(...)
  @user1.vote.create(:value=>##, :votable=>test_object)
  @user2.vote.create(:value=>##, :votable=>test_object)
  @user3.vote.create(:value=>##, :votable=>test_object)
  test_object.votes.score.should == X
end

So, the above works, but its a load of text for each example case, and to iron out the kinks and provide good test coverage I’d like to run about 20 or so test cases.

So, seriously, there must be a simpler way to set this up one time and then test a bunch of possible input/output combinations, right? Can anyone suggest a DRY way to do this kind of test in RSpec?

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-20T23:24:26+00:00Added an answer on May 20, 2026 at 11:24 pm

    Yeah, you could do the following meta-programming to run a series of test that all follow the same format:

    results = { x: ['a', 'b', 'c'], y: ['a','b','c','d'] }
    
    results.each do |score, values|
      it "should have a score of #{score}" do
        test_object = Votable.new(...)
        values.each do |value|
          User.create(...).vote.create(value: value, votable: test_object)
        end
        test_object.votes.score.should == score
      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 model which has a method called 'has_voted'. It looks like this...
I have a model called Vote and this is what vote.rb looks like: class
I have a model called Vote that gets changed very frequently (people voting on
I have a Post model that has a polymorphic association with a Vote model:
I have a Entry model which has a Pushes field and I want each
I have a model Vote , and I'm using an after_validation callback. class Vote
I have a model Vote. Vote has an attribute vote_type. In my controller I
Let's say I have the following classes: class Votable(models.Model): name = ... class Vote(models.Model):
I have a Vote class and one of the properties it can have is
I have a Post, Comment and Vote model Each time an instance of Vote

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.