在Spree Controller Decorator上测试重写的操作

时间:2014-05-21 20:02:21

标签: ruby-on-rails rspec spree

我遇到了测试Spree Controller Decorator的重写操作的问题,如下所示:

app/controllers/spree/checkout_controller_decorator.rb

Spree::CheckoutController.class_eval do
  def update
     do_something
     redirect_to my_other_path
  end
end

我的规格是:

spec/controllers/spree/checkout_controller_spec.rb

describe Spree::CheckoutController do
  routes { Spree::Core::Engine.routes }

  it "does something" do
    put :update
  end
end

但它永远不会进入我的更新定义,它正在进入Spree原始定义。

1 个答案:

答案 0 :(得分:2)

我尝试重现你上面提到的问题。我确实遇到了同样的问题。 但在我查看测试日志之后,对我来说这是抱怨

Filter chain halted as :ensure_checkout_allowed rendered or redirected

因为被覆盖的控制器从未被执行过。它甚至在达到被覆盖的方法之前就失败了。

这是我的spec文件的样子: spec / controllers / checkout_controller_spec.rb

require "spec_helper"

describe Spree::CheckoutController do
  let(:order) {FactoryGirl.create(:spree_order)}

  before(:each) do
    @routes = Spree::Core::Engine.routes
    controller.stub :check_authorization => true
    controller.stub :ensure_checkout_allowed => true
    controller.stub :ensure_valid_state => true
    controller.stub :current_order => order
  end

  it "does something" do
    user = create(:spree_user)
    user.spree_roles.create(:name => "admin")
    sign_in(user)

    puts @routes.inspect
    spree_put :update, :order => {}
    assigns(:test_obj).should eql(1)
  end
end

这是我覆盖的控制器文件的样子: /app/controller/spree/checkout_controller_decorator.rb

Spree::CheckoutController.class_eval do
  def update
    @test_obj = 1
    render :nothing => true
  end
end

您可能需要存储一些before_filters或编写一些代码以使before_filters通过