Testing the addition of SKUs dynamically

Howdy, I’m wondering if anyone has any tips on how to properly test the inclusion of SKUs in a particular attribute. I have a Plan model which has a skus attribute.

After a plan is created and SKUs are assigned, plan.skus could returns something like this:

[
  {
    :sku => "basic_plan_sku",
    :name => "Basic Plan",
  },
  {
    :sku => "addon_sku",
    :price => 100
  },
  {
    :sku => "another_addon_sku",
    :price => 500
  }
]

There are many possibilities here. There could be either more or less SKUs added to a plan… I’d like to be able to say something like, “expect plan.skus to include an array of SKUs”

Is that a good approach? Any tips on how to test for this? I could really use some guidance on this.

I’d need more information to help. What method is it that you are testing? Can you show the class and method you are trying to test? Additionally, what about making Sku a class of its own (a Value Object) rather than a hash?

Sure, I created an assign_sku instance method which determins the SKUs a particular plan should have. For example:

plan = Plan.new( item1: 7, item2: 1, item3: 1 )

plan.assign_skus

plan.skus

[
  {
    :sku => "basic_plan_sku",
    :name => "Basic Plan",
    :item1 => 5 #it includes up to 5 of item1
  },
  {
    :sku => "item1_addon_sku",
    :price => 100
  },
  {
    :sku => "item1_addon_sku",
    :price => 100
  },
  {
    :sku => "item2_addon_sku",
    :price => 500
  }
  {
    :sku => "item3_addon_sku",
    :price => 800
  }
]

I’d really just like to make sure that the instance of Plan gets assigned the correct SKUs. I’ve actually got this all working, but I don’t feel confident enough without solid tests.

I have another instance method called skus_with_quantities which would return something like this:

plan.skus_with_quantities

"basic_sku" => 1,
"item1_addon_sku" => 2,
"item2_addon_sku" => 1,
"item3_addon_sku" => 2

I was going to test with this later though.

What is it about a plan that drives the skus that should be assigned to it? Is this something that could just be done with a database association? Can you give me an example of the code thats in that assign_skus method?

Assumings it’s business logic based on the internal state of a Plan, In order to test it you basically need to exercise however many conditionals it contains, asserting that the after calling the method, skus has exactly the array of skus you would expect. You can use the match_array matcher which makes sure two arrays contain the same elements, regardless of order.