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

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

问题描述

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

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 进行测试并检查价格记录的计数时,它像假设的那样减少了 1.但是,单元测试是否失败.

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

Service 模型中的回调:

The callback in the Service model :

  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.

输出是:前卫服务名称:0前卫创作名称:0在创建或更新价格nombre de service après création :1创作后的荣誉:1nombre de prix avant 更新:1ID10nombre de prix aprè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).to 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?

推荐答案

由于你没有提到你的 ruby​​ 版本,我假设它是 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.在代码中添加断点

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 在由 pry 打开的控制台中(它将是运行 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天全站免登陆