RSpec 控制器规范通过,但控制器中没有相应的实例变量
我编写了一个规范来测试供应商控制器的索引操作中的实例变量@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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为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.