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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:49:50+00:00 2026-05-27T08:49:50+00:00

I’m just getting into TDD with Rails. Something that is puzzling me is when

  • 0

I’m just getting into TDD with Rails. Something that is puzzling me is “when to write the tests”. All guides suggest that you should write the tests before writing any code, but if I create a Person model and then write the following test before writing any code;

p = Person.new
p.firstname = "mikey"
p.lastname = "hogarth"
assert_equal p.fullname, "mikey hogarth"

then the test itself will not fail, it will crash! Because I haven’t implemented the “fullname” method yet, I’ll get a runtime error. I therefore can’t possibly make that test fail until I’ve written the code.

How do TDD coders usually approach this sort of situation? Is it basically with dummy method stubs or is there a better way?

===EDIT===

Lots of great ideas suggested. I eventually decided that the following option achieves what I was trying to do most elegantly;

if p.respond_to? "fullname"
  assert_equal "Mikey Hogarth", p.fullname
else
  flunk "fullname not implemented"
end

===SECOND EDIT===

If you stumble across this answer, it seems my whole approach to TDD was the problem, so while the code above will work it isn’t good practice.

  • 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-27T08:49:50+00:00Added an answer on May 27, 2026 at 8:49 am

    You want to write the code you wish you had. In C style languages/statically compiled languages, the above would not even compile, as you correctly stated the code does not exist. This is fine, you would then implement the bare minimum to make the code build in order to run your tests. In other words, your tests drive your design.

    My Ruby is very rusty but in the above example something along the lines of method_missing will be thrown for the methods/properties that do not exist. Therefore you would create them.

    class Person
      attr_accessor :firstname, :lastname
    
      def fullname
    
      end
    end
    

    If you run your test now you will get nil returned from fullname. Therefore we would implement the fullname method. The point to note here is that message has changed, rather than Ruby moaning about missing methods, the test is moaning that we have not implemented the methods correctly.

    def fullname
       return @firstname + " " + @lastname
    end
    

    Now your test will pass.

    Basically you want to either change the message that your test displays after running (this will prove you are getting somewhere) or you want to make it pass. After the test is passing, you can refactor. The above method is simple, but you could drop the return statement, use string formatting or whatever. As long as the test passes after, you know you’re good to go.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have just tried to save a simple *.rtf file with some websites and
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
We're building an app, our first using Rails 3, and we're having to build

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.