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

  • Home
  • SEARCH
  • 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 7846593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:38:00+00:00 2026-06-02T17:38:00+00:00

My application works fine, but I can’t get a test to pass. I’m new

  • 0

My application works fine, but I can’t get a test to pass. I’m new at rails so forgive me if the answer is obvious.

I need a variable available to every view, so I’m doing this within application_controller.rb:

class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :course
  def course
    @course = Course.find_slug(params[:course])
  end
end

My test case looks like this:

it "creates an attempt" do
  sign_in current_user 
  params = {:id => challenge.id, :description => "this was hard!", :course => "design"}
  @course = FactoryGirl.create(:course)
  post :completed, params
  response.should redirect_to "/#{@course.slug}/?challenge_slug=" + challenge.slug
  Attempt.count.should == 1
  Attempt.last.description.should == params[:description]
end

The method within my controller looks like this:

  def completed
    @challenge = Challenge.find(params[:id])
    @challenge.completed(current_user, params)
    redirect_to "/#{@course.slug}/?challenge_slug=" + @challenge.slug.to_s
  end

All this works fine if I’m using the application, but the test says:

  1) ChallengesController completing a challenge creates an attempt
     Failure/Error: post :completed, params
     NoMethodError:
       undefined method `slug' for nil:NilClass
     # ./app/controllers/challenges_controller.rb:16:in `completed'
     # ./spec/controllers/challenges_controller_spec.rb:36:in `block (3 levels) in <top (required)>'

If I hardcode my controller to say redirect_to "#{'expected_value'}" then the test passes, so it seems that within the testing environment I don’t have access to the application variable @course, is this correct?

I’m lost on how to solve this. Any help is appreciated.

  • 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-02T17:38:02+00:00Added an answer on June 2, 2026 at 5:38 pm

    One solution is to stub the find method and return the instance variable.

    before(:each) do
      @course = FactoryGirl.create(:course)
      Course.stub(:find_slug).and_return(@course)
    end
    

    This makes your tests more robust as the test for “find_slug” should be in your Course model, not the controller.

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

Sidebar

Related Questions

My WPF application works fine on a number of machines, but now I get
I have a web application that works in our stage/test environment fine but once
I put a videoview in my application and it works fine but when you
My application works fine on the dev server but when I upload it fails
This sql command works fine in sqlitemanager but in my android application this don't
I'm developing Swing application, and everything works fine usually. But I have an GUI
I'm writing an application that uses TwainDotNet for scanning. Everything works fine, but scanning
Application works fine. I can access everything in debug except for an Array Adapter
My application works fine locally, but when i'm installing it to production server, i
My ASP.NET web application works fine in Internet Explorer, but in Firefox and Chrome

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.