Skip to content

Instantly share code, notes, and snippets.

@revans
Created September 3, 2014 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save revans/9e184f1e4bc7f7d37240 to your computer and use it in GitHub Desktop.
Save revans/9e184f1e4bc7f7d37240 to your computer and use it in GitHub Desktop.
Rails Fixtures being used for Mocks
require 'ostruct'
require 'yaml'
#
# A quick and dirty example of something that could be inside of rails to provide
# a stupid simple mock for a model object that has an associated fixture.
#
# ==== Example
#
# Fixture.mock("people", "david") # => OpenStruct object representing the person object for David.
#
class Fixture
def self.mock(fixture_file, name)
path = Rails.root.join("test/fixtures").join("#{fixture_file}.yml")
file = File.open(path)
yaml = YAML::load(file)[name.to_s]
OpenStruct.new(yaml)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment