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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:44:00+00:00 2026-05-30T20:44:00+00:00

Given the code below: (1) How would you go about writing a spec to

  • 0

Given the code below:

(1) How would you go about writing a spec to test the :allow_nil => false option?

(2) Is it even worth writing a spec to test?

class Event < ActiveRecord::Base
  belongs_to :league

  delegate :name, :to => :league, :prefix => true, :allow_nil => false
end

describe Event do

  context 'when delegating methods to league object' do
    it { should respond_to(:league_name) }
  end

end

It would actually be nice if you could extend shoulda to do:

it { should delegate(:name).to(:league).with_options(:prefix => true, :allow_nil => false) }
  • 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-30T20:44:01+00:00Added an answer on May 30, 2026 at 8:44 pm

    According to the documentation for the delegate rails module:

    If the delegate object is nil an exception is raised, and that happens no matter whether nil responds to the delegated method. You can get a nil instead with the :allow_nil option.

    I would create an Event object event with a nil league, or set event.league = nil, then try to call event.name, and check that it raises an exception, since that is what is supposed to happen when allow_nil is false (which is also the default). I know rspec has this idiom for exception testing:

    lambda{dangerous_operation}.should raise_exception(optional_exception_class)
    

    I’m not sure if shoulda has this construct, though there are some articles, kinda old, about how to get this behavior in shoulda.

    I think this is worth testing if it is behavior that users of this class can expect or assume will happen – which I think is probably true in this case. I wouldn’t extend shoulda to test “should delegate”, because that seems more implementation-dependent: you’re really saying your Event should raise an exception if you try to call #name when it has a nil league. It’s really unimportant to users of Event how you are making that happen. I would even go so far, if you wish to assert and make a note of this behavior, to test that Event#name has the same semantics as League#name, without mentioning anything about delegate, since this is a behavior-centric approach.

    Build your tests based on how your code behaves, not on how it’s built – testing this way is better documentation for those who stumble into your tests, as they’re more interested in the question “why is my Event throwing?” or “what can cause Event to throw?” than “is this Event delegating?”.

    You can highlight this sort of situation by imagining what failures might happen if you change your code in a way that users of Event shouldn’t care about. If they don’t care about it, the test shouldn’t break when you change it. What if you want to, for example, handle the delegation yourself, by writing a #name function that first logs or increments a counter and then delegates to league? by testing the exception-raising behavior, you are protected from this change, but by testing if Event is a delegate, you will break that test when you make this change – and so your test wasn’t looking at what is really important about the call to #name.

    Anyway, that’s all just soapbox talk. tl;dr: test it if it’s behavior someone might rely upon. Anything that’s not tested is Shroedinger’s cat: broken and not broken at the same time. Truthfully, much of the time this can be OK: It’s a matter of taste whether you want to say something rigorous and definitive about how the system should be behaving, or just let it be “unspecified behavior”.

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

Sidebar

Related Questions

Given the code below, why would I get an error about A's destructor being
Given the code below, how would you create/implement SR.h so that it produces the
Given the code below ... Net::HTTP.start('localhost', 4000) do |http| # # usual stuff omitted
Given the code below, how do I compare a List of objects's values with
I was given the code below to disable a button on an ASP.Net page
Any ideas given the code below why the highlight is being triggered to run
Given the Java code below, what's the closest you could represent these two static
Hello friends i am running code given below which contains the setLogTimeEntery function and
Technology Used:- Asp.Net 2.0 Code:- See Below Description:- hello code given below is working
Given the example source code below, is it possible for someone to see the

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.