Ruby on rails 如何在较大的方法中存根方法响应?
我还没有很好地掌握嘲弄和存根。我将如何着手阻止aodc的成功?调用返回false?AodcWrapper::Order.create未完成,今天将始终返回成功。但我需要模拟失败案例 下面是create方法Ruby on rails 如何在较大的方法中存根方法响应?,ruby-on-rails,rspec,Ruby On Rails,Rspec,我还没有很好地掌握嘲弄和存根。我将如何着手阻止aodc的成功?调用返回false?AodcWrapper::Order.create未完成,今天将始终返回成功。但我需要模拟失败案例 下面是create方法 def create @user = current_user @order = Order.new(order_params) if @order.valid? aodc = AodcWrapper::Order.create(@order) if aodc.su
def create
@user = current_user
@order = Order.new(order_params)
if @order.valid?
aodc = AodcWrapper::Order.create(@order)
if aodc.success?
# pending... Capture the authorization
@order.save
UserMailer.order_received_email(@user).deliver_later
StaffMailer.order_received_email(@user).deliver_later
render json: @order, serializer: Api::V1::OrderSerializer
else
render json: { status: :failure, error: aodc.error_message }
end
else
render json: { status: :failure, error: @order.errors.full_messages }
end
end
这是测试
context "sad path 1: can not connect to the aodc" do
before do
@user = FactoryGirl.create(:user)
@order = FactoryGirl.attributes_for(:shippable_order, user: @user)
sign_in @user
post :create, user_id: @user.id, order: @order
end
it "reponds with a could not connect message" do
parsed_response = JSON.parse(response.body)
expect(parsed_response[:error]).not_to be_nil
end
end
侧面探索。有什么关于资源的建议可以让我去探索,这样我就不会在模仿和存根上吃力了?所以我完全错了。AodcWrapper正在使用HttpParty进行API调用 我解决这个问题的方法是
- 用于记录API交互
- 测试失败,因为响应成功
- 修改VCR盒式磁带yml并将成功响应更改为所需(尚未实现)错误消息
- 重新运行测试,一切正常