Created
September 3, 2014 14:56
-
-
Save revans/9e184f1e4bc7f7d37240 to your computer and use it in GitHub Desktop.
Rails Fixtures being used for Mocks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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