Rspec测试是否存在动作

时间:2010-08-12 08:55:16

标签: ruby-on-rails rspec

我在ROR的Rspec工作.. 我正在尝试使用RSpec测试我的控制器。我有一个带有新功能,标签等功能的用户控制器。

我在spec / users_controller_spec.rb

下创建了一个文件

并将测试用例添加为。

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

获得通过。

但是当我为标签添加测试用例时.. 像

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

这会导致错误

F的

1) 'UsersController GET'标签'应该成功'失败 预期成功?返回true,得到假

为什么会这样?我是ROR的新手,无法找到我收到此错误的原因.. 怎么做这个通行证。 我也试过了Url

http://localhost:3000/users/tags正在为我运行..但是在测试使用$ spec spec /我收到错误..

2 个答案:

答案 0 :(得分:0)

您的测试可能由于多种原因而失败。路由是否需要参数哈希中的ID?控制器动作是重定向的吗?控制器是否出错?

您需要查看控制器代码/和/或routes.rb以找出失败的原因。记下控制器中的before过滤器,这些过滤器可能根本无法访问该行为。

答案 1 :(得分:0)

您需要添加不在默认7条路线范围内的自定义路线。假设您的路线中有var counter = 470; //total length of routine in seconds var secondsLeft = 0; var countDown = setInterval(function(){ counter--; //decrease the counter each second secondsLeft--; // decrease time left in current exercise $(".countdown").html("Just " + secondsLeft + " seconds to go!"); if (counter === 469) { $(".routine").append("<p class='itemName'>Jumping Jacks!</p>"); secondsLeft = 20; } else if (counter === 440) { ... ,则需要对其进行修改。我还假设你的标签路线对于个人用户来说是唯一的。

resources :users

在您的RSpec测试中,您可以将其称为

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

如果路线不是每个用户唯一,则另一个选项是收集路线,如:

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