简体   繁体   中英

Testing Json argument with rspec

I am new to rspec and wanted to ask how one would go about testing the following.

I have an import method that takes in a json feed and my goal is to parse and save certain data based on certain conditions.

for example:

def self.import(json_feed)
  #save certain pieces of data
end

I was wondering what the best way to simulate the json feed is? Should I be creating a json object and saving it into a variable and then passing that in? or are there better conventions for something like this.

Thank You!

class Foo
  class << self
    attr_reader :imported_json
  end

  self.import(json_feed)
    @imported_json = json_feed
  end
end

describe Foo do
  specify 'self.import' do
     some_json = {some: :json}.to_json
     Foo.import some_json
     Foo.imported_json.should == some_json
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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