This Question is Answered

1 "correct" answer available (4 pts) 2 "helpful" answers available (2 pts)
8 Replies Last post: Feb 23, 2013 12:12 AM by Derrell Durrett  
Derrell Durrett Newbie 7 posts since
2013-1-26
Currently Being Moderated

Feb 21, 2013 5:58 AM

Different results between command-line and test execution

I have a several classes that need to be extended to have a representation in JSON. Following the instruction of the JSON docs, and recognizing that I need the same code in many places, I created a pair of modules, one to be included in and one to extend the the class in question. So,

 

 

class Game

  require 'assets/json_serializable'

  require 'assets/json_deserializable'

  include JSONSerializable

  extend JSONDeserializable


  attr_accessor :label


  def initialize(label)

    @label = label

  end


  def json_serialization_list

    [label]

  end

end
and
module JSONSerializable
  require 'json/add/core'
  def to_json(*a)
    {
      json_class: self.class.name,
      data: json_serialization_list
    }.to_json(*a)
  end
end
and
module JSONDeserializable
  def json_create(a)
    new(*a['data'])
  end
end
In irb, using the same (only) ruby on the machine:
1.9.3p385 :002 > require 'app/models/game'
=> true
1.9.3p385 :003 > g = Game.new('7')
=> G7[OID:14415960]: 
1.9.3p385 :004 > g2 = JSON.parse JSON.generate g
=> G7[OID:14336280]: 
1.9.3p385 :005 > g2.eql? g
=> true
whereas in the test:
describe Game do
  before { @game = Game.new('7') }
  subject { @game }
  it "should have a JSON representation" do
    c2 = JSON.parse JSON.generate @game
    c2.should eql @game
  end
end
gives:
expected: G7[OID:32512660]:
     got: {"json_class"=>"Game", "data"=>["7"]}

(compared using eql?)

Diff:
@@ -1,2 +1,3 @@
-G7[OID:32512660]:
+"data" => ["7"],
+"json_class" => "Game"
What am I doing wrong/what should I examine that causes the json representation in the RubyMine to be different in the test than at the command line (in the irb environment)? I've had problems with what I would call a kind of caching behavior, where the results of tests change if I exit RubyMine and restart it. That was not the case here.
Oleg Sukhodolsky JetBrains 388 posts since
2012-4-4
Currently Being Moderated
Feb 21, 2013 11:21 PM in response to: Derrell Durrett
Re: Different results between command-line and test execution

Hi Derrell,

 

I think that this is not about RM but about test framework you are using (it is RSpec or something else?)

I'd expect that the problem is in should or eq matcher.  I'd suggest to debug the test and see what is going wrong.

 

Regards, Oleg.    

Oleg Sukhodolsky JetBrains 388 posts since
2012-4-4
Currently Being Moderated
Feb 22, 2013 11:59 PM in response to: Derrell Durrett
Re: Different results between command-line and test execution

Are you using Gemfile and bundler for the app?  Usually them provide better control on environment.

 

Regards, Oleg.

More Like This

  • Retrieving data ...