在控制器中,它不会分配任何变量进行查看。但是在Rspec测试中,变量总是被分配。
订单#索引
# GET /orders
# GET /orders.json
def index
respond_to do |format|
format.html { render :app }
# format.json { @orders = Order.all.order('created_at desc'); puts 'run'}
end
end
orders_spec.rb
describe "GET #index" do
it "assigns all orders as @orders" do
order = Order.place_order valid_attributes
puts order
# get :index, format: :json
# expect(assigns(:orders)).to eq([order])
get :index, format: 'html'
expect(assigns(:orders)).to eq(nil)
end
end
结果:
Failures:
1) OrdersController GET #index assigns all orders as @orders
Failure/Error: expect(assigns(:orders)).to eq(nil)
expected: nil
got: #<ActiveRecord::Relation [#<Order id: 1, user_id: 1, price: #<BigDecimal:7f9e57da2f30,'0.999E3',9(27)>, created_at: "2015-09-28 04:44:10", updated_at: "2015-09-28 04:44:10", uid: "150928120001">]>
(compared using ==)
# ./spec/controllers/orders_controller_spec.rb:54:in `block (3 levels) in <top (required)>'
答案 0 :(得分:1)
在您的代码中,orders
模板会填充html
。可能是before_filter
或其他内容(例如load_and_authorize_resource
,如果您使用的是cancancan
),无论格式如何,始终都会分配orders
。请确保不是这样。