Monday 15 April 2013

ruby on rails - Testing for FactoryGirl Results -



ruby on rails - Testing for FactoryGirl Results -

i'm trying test see if items array exist after create factory.

spec/models/thing_spec.rb

require 'rails_helper' rspec.describe thing, :type => :model let(:thing) { array.new(3) {factorygirl.create(:thing) } } "should sort items in order" expect(thing).to include(ordering:1, ordering:2, ordering:3) end end

spec/factories/things.rb

factorygirl.define mill :thing, :class => 'thing' name "item_name" sequence(:ordering) end end

below results received.

results

1) things should sorted in order failure/error: expect(thing).to include(ordering:1, ordering:2, ordering:3) expected [#<thing id: 1, name: "item_name", create_date: "2014-11-07 04:18:17", modified_date: "2014-11-14 04:18:17", ordering: 1>, #<thing id: 2, name: "item_name", create_date: "2014-11-07 04:18:17", modified_date: "2014-11-14 04:18:17", ordering: 2>, #<thing id: 3, name: "item_name", create_date: "2014-11-07 04:18:17", modified_date: "2014-11-14 04:18:17", ordering: 3>] include {:ordering => 2} diff: @@ -1,2 +1,19 @@ -[{:ordering=>2}] +[#<thing:0x007fb96217cc30 + id: 1, + name: "item_name", + create_date: fri, 07 nov 2014 04:18:17 utc +00:00, + modified_date: fri, 14 nov 2014 04:18:17 utc +00:00, + ordering: 1>, + #<thing:0x007fb9621cfca0 + id: 2, + name: "item_name", + create_date: fri, 07 nov 2014 04:18:17 utc +00:00, + modified_date: fri, 14 nov 2014 04:18:17 utc +00:00, + ordering: 2>, + #<thing:0x007fb96221eda0 + id: 3, + name: "item_name", + create_date: fri, 07 nov 2014 04:18:17 utc +00:00, + modified_date: fri, 14 nov 2014 04:18:17 utc +00:00, + ordering: 3>]

you can't way. you'll have check each record individually this

it "should sort items in order" expect(thing[0].ordering).to eq(1) expect(thing[1].ordering).to eq(2) expect(thing[2].ordering).to eq(3) end

or this:

it "should sort items in order" expect(thing.map(&:ordering)).to eq([1, 2, 3]) end

you can utilize include check if array includes element whole, this:

expect(thing).to include(thing[0])

ruby-on-rails ruby testing rspec factory-girl

No comments:

Post a Comment