RSpec 控制器规范通过,但控制器中没有相应的实例变量

发布于 2024-12-22 05:11:29 字数 1335 浏览 5 评论 0原文

我编写了一个规范来测试供应商控制器的索引操作中的实例变量@vendors。如果我从供应商控制器中删除@vendors,规范仍然通过。如果控制器中不存在@vendors,任何关于为什么 allocates(:vendors) 都会通过的想法。这是我的代码:

供应商控制器

class VendorsController < ApplicationController
  load_and_authorize_resource

  def index
    # @vendors = Vendor.all
  end

end  

供应商控制器规范

require 'spec_helper'
require 'ruby-debug'

describe VendorsController do
  login_user

  before(:each) do
    @vendor = Factory(:vendor)
  end

  describe "GET index" do
    before(:each) do
      @ability.can :read, Vendor
    end
    it "assigns all vendors to @vendors" do
      get :index
      assigns(:vendors).should == [@vendor]
    end
    it "should render the index template" do
      get :index
      response.should be_success
      response.code.should eq("200")
      response.should render_template("index")
    end
  end
end

供应商工厂< /em>

FactoryGirl.define do
  factory :vendor do |f|
    f.sequence(:name) { |n| "Test#{n}" }
    f.sequence(:address) { |n| "000 Test#{n} drive, Hampton" }
    f.state "Virginia"
    f.zip "00000"
    f.sequence(:telephone) { |n| "000-000-000#{n}" }
    f.sequence(:poc) { |n| "Test#{n}" }
  end
end

谢谢

I wrote a spec to test the instance variable @vendors in the index action of my vendors controller. If I remove @vendors from the vendors controller the spec still passes. Any ideas as to why assigns(:vendors) would pass if @vendors doesn't exists in the controller. Heres my code:

Vendors Controller

class VendorsController < ApplicationController
  load_and_authorize_resource

  def index
    # @vendors = Vendor.all
  end

end  

Vendors Controller Spec

require 'spec_helper'
require 'ruby-debug'

describe VendorsController do
  login_user

  before(:each) do
    @vendor = Factory(:vendor)
  end

  describe "GET index" do
    before(:each) do
      @ability.can :read, Vendor
    end
    it "assigns all vendors to @vendors" do
      get :index
      assigns(:vendors).should == [@vendor]
    end
    it "should render the index template" do
      get :index
      response.should be_success
      response.code.should eq("200")
      response.should render_template("index")
    end
  end
end

Vendors Factory

FactoryGirl.define do
  factory :vendor do |f|
    f.sequence(:name) { |n| "Test#{n}" }
    f.sequence(:address) { |n| "000 Test#{n} drive, Hampton" }
    f.state "Virginia"
    f.zip "00000"
    f.sequence(:telephone) { |n| "000-000-000#{n}" }
    f.sequence(:poc) { |n| "Test#{n}" }
  end
end

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独患者 2024-12-29 05:11:29

因为load_and_authorize_resource实际上是加载并授权的。

所以你的代码是不必要的。

您可以使用authorize_resource进行更改,因此规范将失败。

Because load_and_authorize_resource actually loads and authorizes.

So your code is unnecessary.

You could change with authorize_resource, thus the spec will fail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文