Rspec和Rails 4,更新跳过回调 [英] Rspec and Rails 4, update skip callback

查看:56
本文介绍了Rspec和Rails 4,更新跳过回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试对我的Rails应用程序使用rspec运行更新测试.

I try to run an update test with rspec for my rails application.

我正在使用rspec 3.5和rails 4.

I'm using rspec 3.5 and rails 4.

该行为应为以下情况:

当我创建一个具有销售价格的新服务时,它会创建一个Price实例并设置与该服务的关系.然后,当我更新服务时,如果没有销售价格,则会破坏价格记录(要求客户节省数据库空间). 我执行的过程似乎运行良好,当我使用UI进行测试并检查Price记录的数量时,它的数量就减少了一个.但是,如果失败,则进行单元测试.

When i create a new service with a selling price, it's create a Price instance and set the relation with the service. Then, when i update my service, if there is no selling price, it's destroy the price record (requirement of the client to save space in database). The process i implemented seems to be working fine, when i test with the UI and i check the count of Price record, it's decrease by one like it's suppose. However, the unit test if failing.

这是代码:

服务控制器:

  def update
    @service.assign_attributes(service_params)

    puts "update method"

    respond_to do |format|
      if @service.valid?
        if params['preview']
          @service.build_previews
          format.js { render 'services/preview' }
        else
          @service.save!
          format.html { redirect_to client_trip_days_path(@client, @trip), notice: t('flash.services.update.notice') }
        end
      else
        format.html { render :edit }
        format.js { render :new }
      end
    end
  end

服务模型中的回调:

  def create_or_update_price
    puts "in create or update price"
    if selling_price.present? && price.present?
      self.price.update_columns(:trip_id => trip.id, :currency => trip.client.currency, :purchase_price => purchase_price, :selling_price => selling_price)
    elsif selling_price.present? && !price.present?
      self.price = RegularPrice.create(:trip => trip, :currency => trip.client.currency, :purchase_price => purchase_price, :selling_price => selling_price)
    elsif !selling_price.present? && price.present?
      self.price.destroy
    end
  end

测试:

it "updates the lodging and destroy the price" do
    puts "nombre de service avant création : "
    puts Service.count
    puts "nombre de prix avant création : "
    puts Price.count
    lodging = FactoryGirl.create(:lodging_service, selling_price: 200)
    puts "nombre de service après création : "
    puts Service.count
    puts "nombre de prix après création : "
    puts Price.count
    expect(lodging.reload.price).to be_present
    puts "nombre de prix avant update : "
    puts Price.count
    puts "id"
    puts lodging.id
    patch :update, client_id: client.id, trip_id: trip.id, id: lodging.to_param, service: valid_attributes_no_more_price
    # patch :update, client_id: client.id, trip_id: trip.id, id: lodging.id, service: valid_attributes_with_price
    puts "nombre de prix après update : "
    puts Price.count
    # expect{
    #   patch :update, client_id: client.id, trip_id: trip.id, id: lodging.id, service: valid_attributes_no_more_price
    # }.to change(RegularPrice, :count).by(0)
    expect(lodging.reload.price).to be_nil
  end

  let(:valid_attributes_no_more_price) {
attributes_for(:lodging_service, trip: trip, selling_price: "")

}

如您所见,由于我尝试找出问题所在,因此有很多看跌期权.

As you can see, there is a lot of puts since i try to find what is wrong.

输出为: nombre de service avant创作: 0 nombre de prix avant创作: 0 在创建或更新价格 nombre de serviceaprèscréation: 1个 nombre de prixaprès创作: 1个 nombre de prix avant更新: 1个 ID 10 nompri de prixaprès更新: 1

The output is : nombre de service avant création : 0 nombre de prix avant création : 0 in create or update price nombre de service après création : 1 nombre de prix après création : 1 nombre de prix avant update : 1 id 10 nombre de prix après update : 1

失败/错误:expect(lodging.reload.price).be_nil

Failure/Error: expect(lodging.reload.price).to be_nil

   expected: nil
        got: #<RegularPrice id: 2, label: nil, currency: "CHF", selling_price: 200.0, priceable_type: "Service", p...ated_at: "2017-07-13 08:08:47", quantity: 1, type: "RegularPrice", position: 1, purchase_price: nil>

我们可以看到,更新后似乎未触发回调,并且未达到控制器中的动作.

As we can see, it's look like the callback is not fired after the update, and the action in the controller is not reached.

您知道发生了什么问题吗?

Have you any idea what is going wrong?

谢谢:)

PS:我总是很难在问题中包含代码,是否有关于如何编写代码的教程?

PS: I always have trouble to include code in my questions, is there a tutorial on how to make it?

推荐答案

由于您没有提到红宝石版本,因此我认为它是2.

Since you did not mentioned your ruby version I'll assume it's 2.

首先,您需要学习如何正确调试代码以自己解决问题.

First of all you need to learn how to properly debug your code in order to fix the issues yourself.

这是您要做的:

1.将pry gem添加到您的应用中, pry-byebug 还是ruby 1.9.3的版本.

1.Add pry gem to your app, pry-byebug there is also a version for ruby 1.9.3.

2.在代码中添加一个断点

2.Add a break point in your code

it "updates the lodging and destroy the price" do
  (...) # Code here.

  binding.pry # The debugger  will open a console at this point   

  (...) # Maybe more code here
end

3.验证所有变量及其值,然后查看问题出在哪里

3.Verify all the variable and their values and see where the problem lies

如果您在rspec文件中找不到binding.pry的问题,而在期望值之前将其添加到方法的第一行之后,则可以通过在打开的控制台中键入next来逐步调试程序撬开(它将在您的rails服务器运行的位置). 如果仍然不能解决问题,请尝试在类中添加binding.pry,然后查看其中的状态.

In case you could not find the issue with binding.pry in your rspec file before the expect add it to after the first line of the method, and you can step through the debugger by typing next in the console opened by pry (it will be where your rails server is runnig). If that still does not help, try and add a binding.pry in your classes and see what is the state in there.

现在花数分钟/小时来学习调试,从长远来看,它将为您节省几天/几周的时间. (在学习时,您实际上并不知道程序应该如何运行,而额外的知识是无价的,过了一会儿,您只需要调试器即可解决极其复杂的问题).

Spend a few minutes/hours now to learn debugging and it will save you days/weeks in long term. (while you are learning you don't really know how a program should behave and that extra knowledge is priceless, after a while you only need a debugger for extremely complicated issues).

这篇关于Rspec和Rails 4,更新跳过回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆