我在使用get方法时测试没有通过,但是当我使用put方法时,对于相同的代码库是绿色的。我不知道如何解决这个问题,需要帮助才能通过测试。
我收到200 HTTP
状态代码,但我希望获得有关重定向的确认,以便rspec
跟踪它。在尝试使用category_product_url(category, product)
编辑产品时,基本上需要执行的操作是将非产品所有者的登录用户重定向到error: 'You are not allowed to edit this product.'
并使用闪存get http method
。
二手宝石rspec-rails
,devise
和decent_exposure
。 Ruby 2.1.5
和Rails 4.1.8
实际错误消息:
Failure/Error: expect(response).to redirect_to(category_product_url(category, product))
Expected response to be a <redirect>, but was <200>
Failure/Error: expect(controller.flash[:error]).to eq 'You are not allowed to edit this product.'
expected: "You are not allowed to edit this product."
got: nil
(compared using ==)
我的规格
context 'another user is singed in' do
let(:user) { create(:user) }
let(:user2) { build(:user) }
let(:product) { Product.create! valid_attributes }
before do
sign_in user2
controller.stub(:user_signed_in?).and_return(true)
controller.stub(:current_user).and_return(user2)
controller.stub(:authenticate_user!).and_return(user2)
product.user = user
end
describe 'GET edit' do
describe 'with valid params' do
it 'redirects to product page' do
get :edit, { id: product.to_param, category_id: category.to_param }
expect(response).to redirect_to(category_product_url(category, product))
end
it 'renders error message' do
get :edit, { id: product.to_param, category_id: category.to_param }
expect(controller.flash[:error]).to eq 'You are not allowed to edit this product.'
end
end
end
我的控制器
before_action :authenticate_user!, only: [:new, :edit, :update, :destroy, :create]
expose(:category)
expose(:products)
expose(:product)
def edit
end
def update
if product.user == current_user
if self.product.update(product_params)
redirect_to category_product_url(category, product), notice: 'Product was successfully updated.'
else
render action: 'edit'
end
else
redirect_to category_product_url(category, product), flash: { error: 'You are not allowed to edit this product.' }
end
end
private
def product_params
params.require(:product).permit(:title, :description, :price, :category_id, :user_id)
end
奇怪的是,put方法在使用相同的update
操作时工作正常。以下规格正在传递
describe 'PUT update' do
describe 'with valid params' do
it 'redirects to product page' do
put :update, { id: product.to_param, product: { 'title' => 'MyString' }, category_id: category.to_param }
expect(response).to redirect_to(category_product_url(category, product))
end
it 'does not update product' do
put :update, { id: product.to_param, product: { 'title' => 'MyNewString' }, category_id: category.to_param }
expect(controller.product.title).to_not eq 'MyNewString'
end
it 'renders error message' do
put :update, { id: product.to_param, product: { 'title' => 'MyString' }, category_id: category.to_param }
expect(controller.flash[:error]).to eq 'You are not allowed to edit this product.'
end
end
end
答案 0 :(得分:1)
decent_exposure
和devise
的工作方式,您需要先调用before_action
方法,然后通过您选择的私有方法。
对于此特定示例,您的控制器应包含此
before_action :author!, only: [:edit, :update]
和私有方法过滤器
def author!
unless self.product.user == current_user
redirect_to category_product_url(category, product),
flash: { error: 'You are not allowed to edit this product.' }
end
end
通过这种方式,您可以获得get http requests
和put http requests
规范。
答案 1 :(得分:0)
试试这个
expect(:get => "/products/1/edit").to route_to(:controller => "controller_name", :action => "action_name")
希望这会奏效。