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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:47:11+00:00 2026-05-25T00:47:11+00:00

I often see the code which uses mock in Rspec, like this: describe GET

  • 0

I often see the code which uses mock in Rspec, like this:

describe "GET show" do
  it "should find and assign @question" do
    question = Question.new

    Question.should_receive(:find).with("123").and_return(question)
    get :show, :id => 123

    assigns[:question].should == question
  end
end

But why they don’t add a Question with id => 123 in database, retrieve it by get, and destroy it? Is this a best practice? If I don’t follow the rule, will something bad happen?

  • 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-25T00:47:12+00:00Added an answer on May 25, 2026 at 12:47 am

    When you write a behavioral test (or a unit test), you’re trying to test only a specific part of code, and not the entire stack.

    To explain this better, you are just expressing and testing that “function A should call function B with these parameters”, so you are testing function A and not function B, for which you provide a mock.

    This is important for a number of reasons:

    1. You don’t need a database installed on every machine you build your code, this is important if you start using build machines (and/or continuous integration) in your company with hundreds of projects.
    2. You get better test results, cause if function B is broken, or the database is not working properly, you don’t get a test failure on function A.
    3. Your tests run faster.
    4. It’s always a pain to have a clean DB before each test. What if a previous run of your tests was stopped, leaving on the database a Question with that id? You’ll probably get a test failure because of duplicate id, while in reality the function is working properly.
    5. You need a proper configuration before running your test. This is not such an incredible problem, but it’s much better if tests can run “out of the box”, without having to configure a database connection, a folder of temporary test files, an SMTP server for testing email stuff, etc…

    A test that actually test the entire stack is called “end to end testing”, or “integration testing” (depending on what it tests). These are important as well, for example a suite of tests without mock database can be used to see if a given application can run safely of a different DB than the one used during development, and eventually fix functions that contain offending SQL statements.

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

Sidebar

Related Questions

I'm still learning ASP.NET and I often see code like this throughout parts of
I often see JavaScript code which checks for undefined parameters etc. this way: if
I often see code like that which is shown here , ie where an
I often find I want to write code something like this in C#, but
I often see code like: Iterator i = list.iterator(); while(i.hasNext()) { ... } but
I often see code like int hashCode(){ return a^b; } Why XOR?
You often see, on sites like The Daily WTF , examples of overengineered code
I've see this sort of thing in Java code quite often... try { fileStream.close();
I often see this line of code [super awakeFromNib] in the awakeFromNib method in
I find this code useful and want to copy-paste it into my application which

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.