开发者

Really weird problem about getting a rspec controller test to pass where the code is correct

开发者 https://www.devze.com 2023-03-06 06:48 出处:网络
I have a weird problem where I know the code works, the RSpec test will pass if I run that Spec file by itself, but it fails when I run all the tests in the entire suite (everything in /specs).

I have a weird problem where I know the code works, the RSpec test will pass if I run that Spec file by itself, but it fails when I run all the tests in the entire suite (everything in /specs).

Here is the test:

require 'spec_helper'

describe WebpagesController do
  include Devise::TestHelpers

  render_views

  describe "GET 'show'" do
    it "should render the template if it exists" do
      get 'show', :page => "tour"

      response.should render_template("tour")
    end

    it "should render 404 page if template does not exist" do
      expect {
        get 'show', :page => 'does_not_exist'
      }.to_not raise_error(ActionView::MissingTemplate)

      response.should render_template("/public/404")
    end
  end

end

Here's the code:

class WebpagesController < ApplicationController

  def show
    begin
      render(params[:page])
    rescue ActionView::MissingTemplate
      render("/public/404")
    end
  end

end

The idea here is that the 'show' action should render the template with whatever name is given by the parameter, but if it doesn't exist, we want to send the user to the generic 404 page.

Now, I could just duplicate the 404 template in the /webpages view directory, but I really want to figure out how I can get this to pass usi开发者_StackOverflowng the one provided in the /public folder like I am trying to do here.

If I run the test in isolation - it PASSES. If I run the test with all the others, I get the following error:

expected no ActionView::MissingTemplate, got #<ActionView::MissingTemplate: Missing template /public/404 with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]} in view paths "/home/egervari/Projects/training/app/views", "/usr/local/lib/ruby/gems/1.9.1/gems/devise-1.3.4/app/views", "/home/egervari/Projects/training/spec", "/">
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-expectations-2.5.0/lib/rspec/expectations/fail_with.rb:29:in `fail_with'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-expectations-2.5.0/lib/rspec/expectations/handler.rb:44:in `handle_matcher'
/usr/local/lib/ruby/gems/1.9.1/gems/rspec-expectations-2.5.0/lib/rspec/expectations/extensions/kernel.rb:50:in `should_not'
/home/egervari/Projects/training/spec/controllers/webpages_controller_spec.rb:17:in `block (3 levels) in <top (required)>'

I've honestly been stumped with this one for several days, and I've just been working on other stuff... but I am a little annoyed to see 1 test failing all the time even though I personally know it's fine.

Thanks for the help


The error is actually saying that your render('/public/404') line is the one throwing the error.

Try rendering the full path to the 404 file instead of just /public/404/:

render("#{Rails.root}/public/404.html")
0

精彩评论

暂无评论...
验证码 换一张
取 消