开发者

Rspec test for the existence of an action is not working

i am working in Rspec of ROR.. I am trying to test my controllers using RSpec.i am having a Users controller with functions like new , tags, etc..

i created a file under spec/users_controller_spec.rb

and added the test cases as.

require 'spec_helper'

describe UsersController do
  integrate_views

  it "should use UsersController" do
    controller.should be_an_instance_of(UsersController)
  end


  describe "GET 'new'" do
    it "should be successful" do
      get 'new'
      response.should be_success
    end

    it "should have the title" do
      get 'new'
      response.should have_tag("title", "First app" )
    end
  end
end

which gets pass.

But when i add a test case for tags .. like

  describe "GET 'tags'" do
    it "should be successful" do
      get 'tags'
      response.should be_success
    end
  end

this results in an error as

F...

1) 'UsersController GET 'tags' should be successful' FAILED expected success? to return true, got false

why it is coming like this ?? i am very new to ROR and cant find the reason of why i am getting this error.. How to make this pass . Also i tried the Url

http://localhost:300开发者_StackOverflow中文版0/users/tags which is running for me .. But on testing using $spec spec/ i am getting the error ..


Your test may be failing for any number of reasons. Does the route require an ID in the parameter hash? Is the controller action redirecting? Is the controller raising an error?

You'll need to look at the controller code /and/or routes.rb to discover the cause of the failure. Take note of before filters in the controller, which may not allow the action to be reachable at all.


You need to add custom routes that are not within the default 7 routes. Assuming you have resources :users within your routes you will need to modify it. I'm also assuming that your tags route is unique to individual users.

resources :users do
  member do
    # creates /users/:user_id/tags
    get :tags
  end
end

And in your RSpec test you would call it like

describe '#tags' do
  user = create :user
  get :tags, user_id: user.id
end

If the route is not to be unique per user the other option is a collection route, something like:

resources :users do
  collection do
    # creates /users/tags
    get :tags
  end
end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜