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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:59:16+00:00 2026-06-01T10:59:16+00:00

Is it possible (and reasonable) to write Controller tests and classes before writing the

  • 0

Is it possible (and reasonable) to write Controller tests and classes before writing the underlying Model classes? I thought I saw notes on how to do that, but now I can’t find the recipe.

For example, consider the following controller:

# file: app/controllers/premises_controller.rb
class PremisesController < ApplicationController

  def create
    @premise = Premise.new(params[:premise])
    respond_with @premise
  end

end

Can I test this controller code before creating the underlying Premise model and premises? I know the following won’t work — how would you re-write it (if it’s possible)?

# file: spec/controller/premise_spec.rb
require "spec_helper.rb"

describe PremisesController do
  context 'POST create' do
      it 'should assign a new Premise to @premise' do
        premise = stub_model(Premise)
        Premise.stub(:create) { premise }
        post :create
        assigns(:premise).should == premise
      end
    end
  end
end

update

The more I think about it, the more I’m convinced that I do need to define the Premise class — the PremisesController code needs to reference it. So I’ll change my question to “is it necessary to create the underlying premises database table in order to run the PremisesController tests?”

At this point, I don’t see a good way around it (without changing the PremisesController code, which defeats the point of testing). For example, the call to respond_with calls @premise.has_errors? which in turn accesses the database to fetch the column names. Unless I’m willing to stub methods internal to ActiveRecord, I don’t see how to avoid a hit to the DB.

But I’d love to be shown otherwise.

  • 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-06-01T10:59:18+00:00Added an answer on June 1, 2026 at 10:59 am

    Okay – I’ve made my peace with this: it’s not practical to create any meaningful tests unless the database table exists — too many bits of ActiveRecord depend on having the table defined.

    But this doesn’t prevent writing test that with a clean separation between the controller and the model. This is eloquently covered by dchelimsky Himself in this RSpec issue. The gist of his post:

    • Test using an integration test in spec/requests/premise_spec.rb which issues a get and verifies the generated json response.
    • With a skinny controller method (such as shown in the OP), don’t bother writing a controller test.
    • Write model tests to verify that the model is emitting the correct json.

    By the way, I really recommend reading David’s post — he takes you through a step-by-step process, explaining the philosophy and the reasoning behind each step.

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

Sidebar

Related Questions

Possible Duplicate: Yield In VB.NET In C#, when writing a function that returns an
Is it possible to solve a problem of O(n!) complexity within a reasonable time
Possible Duplicate: Where do the Python unit tests go? Are unit tests kept in
What are the current rules for writing python code that will pass cleanly through
I am trying to write a stand alone Class that can be imported. As
I think is it quite possible, but I'm not sure. I don't have the
Possible Duplicate: Obtaining const_iterator from iterator I want to write a metafunction which returns
I'm cleaning up some pretty complicated code that I didn't write and I'm looking
Is it possible/feasible/reasonable to use HAProxy to load-balance three SQL Server 2008 database servers?
I sense that the Scala community has a little big obsession with writing concise,

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.