我从昨天开始就陷入了对rspec的问题,我开始参加测试,所以我在理解问题时遇到了一些麻烦。我使用Prwan的pdf视图为pdf格式的订单提供发票,我以这种方式限制了对控制器的访问:
order_token = params[:order_token]
if order_token != @order.guest_token || order_token == nil || @order.guest_token == nil
permission_denied
end
我想创建一个测试,验证如果我没有发票,我看不到它,我的测试写得像这样:
RSpec.describe Spree::OrdersController, type: :controller do
let(:user) { create(:user, password: "12345678", password_confirmation: "12345678", email: "test@gmail.com") }
let(:order) { create(:order, user: user, total: 100.0) }
context "#Missing token" do
it "should return unauthorized" do
get :invoice, params: {id: order.number, format: :pdf}
puts response.inspect
assert_response 401
end
end
end
我在运行测试时有一个,我有一个我不理解的重定向!
1) Spree::OrdersController#Missing token should return unauthorized
Failure/Error: assert_response 401
Minitest::Assertion:
Expected response to be a <401: Unauthorized>, but was a <302: Found> redirect to <http://test.host/login>.
Expected: 401
Actual: 302
# ./spec/controllers/spree/frontend/orders_controller_decorator_spec.rb:11:in `block (3 levels) in <top (required)>'
Finished in 14.91 seconds (files took 7.99 seconds to load)
1 example, 1 failure