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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:14:32+00:00 2026-05-23T16:14:32+00:00

I have a module like this (but more complicated): module Aliasable def self.included(base) base.has_many

  • 0

I have a module like this (but more complicated):

module Aliasable 
  def self.included(base)
    base.has_many :aliases, :as => :aliasable
  end
end

which I include in several models. Currently for testing I make another module as below, which I just include in the test case

module AliasableTest 
  def self.included(base)
    base.class_exec do 
      should have_many(:aliases)
    end
  end
end

The question is how do I go about testing this module in isolation? Or is the above way good enough. Seems like there is probably a better way to do it.

  • 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-23T16:14:32+00:00Added an answer on May 23, 2026 at 4:14 pm

    First off, self.included is not a good way to describe your modules, and class_exec is needlessly complicating things. Instead, you should extend ActiveSupport::Concern, as in:

    module Phoneable
      extend ActiveSupport::Concern
    
      included do
        has_one :phone_number
        validates_uniqueness_of :phone_number
      end
    end
    

    You didn’t mention what test framework you’re using, but RSpec covers exactly this case. Try this:

    shared_examples_for "a Phoneable" do
      it "should have a phone number" do
        subject.should respond_to :phone_number
      end
    end
    

    Assuming your models look like:

    class Person              class Business
      include Phoneable         include Phoneable
    end                       end
    

    Then, in your tests, you can do:

    describe Person do
      it_behaves_like "a Phoneable"      # reuse Phoneable tests
    
      it "should have a full name" do
        subject.full_name.should == "Bob Smith"
      end
    end
    
    describe Business do
      it_behaves_like "a Phoneable"      # reuse Phoneable tests
    
      it "should have a ten-digit tax ID" do
        subject.tax_id.should == "123-456-7890"
      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 method in a url rewriting module that looks like this public
I have a GWT module with the X-GWT-Module-Base http://host:8080/foo/ and would like to call
I posted a question about this earlier, but I have more information now and
I have a Perl module that I would like to use from Java. Is
I have a Perl module and I'd like to be able to pick out
I have a Python module installed on my system and I'd like to be
I have an executable module created by third party. I would like to inject
I got a Utility module since VB.NET doesn't have static class like C# and
There are a few similar questions, but nothing like this. How do you deal
I have module application. When I run it, the main window of that app

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.