简体   繁体   English

访问其他类的变量

[英]Accessing variables of other classes

I received some excellent help on my last post ( Undefined Method in rspec testing ) but I was just looking for a bit more help. 我在上一篇文章( rspec测试中的Undefined Method )上获得了一些出色的帮助,但我只是在寻找更多帮助。

I have an rspec integration spec that I basically need to alter code for to get the desired outcome. 我有一个rspec集成规范,我基本上需要更改代码以获得所需的结果。 I cannot alter the spec as it's part of the exercise. 我不能更改规格,因为它是练习的一部分。

let(:user) { User.new(voucher) }

context 'no voucher' do
  let(:voucher) { nil }

  it 'should bill default price all the time' do
      user.bill
      expect(user.orders[0].billed_for).to eql 6.95
      ... ...
  end
end

context 'vouchers' do
  describe 'default vouchers' do
    let(:voucher) { Voucher.create(:default, credit: 15) }

    it 'should not bill user if has a remaining credit' do
      user.bill
      expect(user.orders[0].billed_for).to eql 0.0
      ... ...
    end
  end

I placed some dots just to cut out the unnecessary code. 我放了一些点只是为了切掉不必要的代码。

I pretty much understand what's happen here. 我几乎了解这里发生的事情。

A new user class is being create and set to :user in let. 正在创建一个新的用户类,并在let中将其设置为:user。 A voucher is then initiliased and passed in depending on the context. 然后根据上下文初始化凭证并传递凭证。 no voucher is set for the first test. 没有为第一次测试设置优惠券。 One is set for the second. 一个设置为第二个。

Here's where my questions begin 这是我的问题开始的地方

require 'order'
require 'voucher'

class User
  attr_accessor :voucher, :orders

  def initialize(orders = [], voucher = nil)
    @voucher = voucher
    @orders = [orders]
  end

  def bill
    new_order = Order.new(self)
    @orders << new_order
  end
end

The method is initliased. 该方法已初始化。 It has optional parameters. 它具有可选参数。 I'm a little unclear on how the initialisation works though as I'm unable to access these set variables at all anywhere. 我还不清楚初始化的工作方式,因为我无法在任何地方访问这些设置的变量。

I'm a little unsure about the scope limitations though as I'm hoping to access some of the vouchers variables from the order class which currently looks like this 我不确定范围的限制,虽然我希望从订单类中访问一些凭证变量,当前看起来像这样

class Order
  DEFAULT_PRICE = 6.95

  attr_accessor :user

  def initialize(user)
    @user = user
  end

  def billed_for
    price = DEFAULT_PRICE
    user.orders.each do |order|
        price - order.billed_for
    end
    price
  end
end

shoudl accessing the users voucher class be as easy as user.voucher. 应该像user.voucher一样容易地访问用户凭证类。 ?? ??

Also a smaller question. 还有一个较小的问题。 I'm currently using a factory method so the voucher class can initialise itself. 我当前使用的是工厂方法,因此凭证类可以自行初始化。

 def self.create(type, *attrs)

the *attrs parameter is essentially an array. * attrs参数本质上是一个数组。 I can loop through this and bind it to some expected variables by checking for their presence. 我可以遍历此过程,并通过检查它们的存在将其绑定到某些预期变量。 ie if array has certain key set this key's value to a variable. 即,如果数组具有某些键,则将该键的值设置为变量。 Is this the best way or is there another popular way? 这是最好的方法还是还有另一种流行的方法?

I know this is a lot to ask but I'm finding myself really confused and would be grateful if annyone could clear any of this up me. 我知道有很多问题要问,但我发现自己真的很困惑,如果安妮妮能清除我身上的任何东西,我将不胜感激。 Thanks. 谢谢。

For the User class you can access the voucher and orders variables as they are instance variables and publicly accessible due to the attr_accessor at the top. 对于User类,您可以访问voucherorders变量,因为它们是实例变量,并且由于位于顶部的attr_accessor而可以公开访问。

For your factory problem I recommend FactoryGirl from Thoughbot it makes building Factory objects very easy. 对于您的工厂问题,我推荐Whilebot的FactoryGirl,它使构建Factory对象非常容易。 You can also use Rails Fixtures which are static data you create. 您也可以使用Rails 固定装置 ,它们是您创建的静态数据。

Turns out I was initializing the variables in the wrong order. 原来我以错误的顺序初始化变量。 This meant that I was looking for items in the wrong class. 这意味着我在寻找错误类中的项目。

Embarrassing noob mistake! 令人尴尬的菜鸟错误! All the better for it :) 更好的是它:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM