Rails 4 Minitest测试虚拟属性 [英] Rails 4 Minitest Testing Virtual Attributes

查看:111
本文介绍了Rails 4 Minitest测试虚拟属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个TimeBank类,并且正在对其进行更新,并且正在获取两个新的虚拟属性,这些属性将操纵另外两个现有属性(数据库列).

So I have a class TimeBank, and its being updated, and is getting two new virtual attributes which will manipulate two other existing attributes (DB columns).

class TimeBank < ActiveRecord::Base
  # validations, consts, few other methods omitted for brevity

  def date_worked
    @date_worked ||= self.start.to_date
  rescue
    Date.current
  end

  def date_worked=(date_worked_val)
    self.attributes['date_worked'] = date_worked_val
  end

  def hours_worked
    @hours_worked ||= hours
  rescue NoMethodError
    nil
  end

  def hours_worked=(hours_worked_val)
    self.attributes['hours_worked'] = hours_worked_val
  end

  def hours
    if attributes["hours"]
      read_attribute(:hours).to_f 
    else
      ((finish - start)/1.hour).to_f
    end
  rescue NoMethodError
    0.0
  end

  # other methods omitted for brevity...
end

使用该应用程序时,一切正常.在运行测试时,任何时候我实例化TimeBank时,都只会得到上面的rescue语句的结果,即date_worked的今天日期和hours_worked0.0日期.设置器似乎没有反应,就像他们什么都不做(在测试中).

Everything works fine when using the application. When running tests, any time I instantiate a TimeBank, I only get the results of the rescue statements above, namely, today's date for date_worked and 0.0 for hours_worked. The setters seem unresponsive, like they do nothing (while in tests).

我还定义了设置器以使用write_attribute(:date_worked, 'SOMEVAL'),虽然在应用程序中可以使用,但是对于测试,我会遇到类似

I had also defined the setters to use write_attribute(:date_worked, 'SOMEVAL') and while that works within the application, for tests, I get errors like

ActiveModel::MissingAttributeError: can't write unknown attribute hours_worked'
app/models/time_bank.rb:110:in `hours_worked='

一个测试示例如下:

describe "virtual attributes test" do
  subject { TimeBank.new({member: @gus, admin_id: @addy.id, date_worked: Date.yesterday, hours_worked: 2.5, time_type: "store_shift", approved: true}) }

  it "tests setter callbacks" do
    subject.date_worked.must_equal Date.yesterday
    subject.hours_worked.must_equal 2.5
  end
end

这两个断言总是失败,因为它们得到今天的日期和0.0(getter中指定的默认值").如果我在测试之前立即在行上设置属性,它仍然无效.

Both assertions always fail, since they get today's date and 0.0 (the "defaults" as specified in the getters). If I set the attributes on the line immediately before the tests, it still has no effect.

我不确定自己在做什么错.我想为这2个虚拟属性编写测试,但是对它们的值进行任何设置似乎都无效.我不确定为什么它在测试中不起作用,但是在应用程序中能正常工作.相同的代码在应用程序控制器中按预期工作.谁能帮我理解为什么?

Im not sure what Im doing wrong. Id like to write tests for these 2 virtual attributes, but any setting of their values seems to have no effect. Im not sure why it doesnt work within tests, but works fine within the application. The same code works as expected within the application controllers. Can anyone help me understand why?

推荐答案

问题出在setter方法之内.除了为self.attributes["virtual_attribute"]分配内容外,还需要设置并返回一个实例变量,如下所示:

The problem lies within the setter methods. Instead of assigning something to self.attributes["virtual_attribute"], an instance variable is needed to be set and returned, like so:

def virtual_attribute=(virtual_attr_value)
  @virtual_attribute = virtual_attr_value
end

因此,对于我的示例,我需要:

So for my example, I needed:

def hours_worked=(hours_worked_val)
  @hours_worked = hours_worked_val
end

这两个应用程序以及我在minitest中进行的getter/setter测试现在都表现出预期的效果.

Both the application, and my getter/setter tests in minitest, behave as expected now.

这篇关于Rails 4 Minitest测试虚拟属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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