Coder Social home page Coder Social logo

rspec-file_fixtures's Introduction

RSpec::FileFixtures

A simple and convenient file fixture loader for RSpec.

Installation

Add this line to your application's Gemfile:

gem 'rspec-file_fixtures', '~> 0.1.6'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install rspec-file_fixtures

Usage

Load the gem in your test environment (e.g. in spec/spec_helper.rb):

require 'rspec/file_fixtures'

Use the fixture helper method to load a fixture from spec/fixtures:

let(:my_fixture) { fixture('fixture.json') }

Use the returned Fixture object's various methods in your tests:

it 'parses JSON data' do
  expect(subject.load(my_fixture.read)).to eql my_fixture.from_json
end
it 'contains same filenames as fixture directory' do
  # `#dirname` delegated to underlying `Pathname` object, returning a new `Pathname` instance:
  expect(subject.created_files).to eql my_fixture.dirname.entries
end

The following methods are provided on the Fixture object:

Method Definition
#read Read the contents of the fixture file as a string
#path The absolute path to the fixture file
#from_json The parsed JSON content from the file
#from_yaml The parsed YAML content from the file (aliased as #from_yml)
#from_xml The parsed XML content from the file (requires the Nokogiri gem and returns a Nokogiri::XML::Document)

Methods not specifically defined above are delegated to the underlying Pathname object (#join, #dirname, #mkpath, etc.). See the Pathname documentation for more details.

Symbolize Names

By default Fixture#from_yaml and Fixture#from_json symbolize all keys in their output. To disable this behaviour, pass symbolize_names: false to either method. A shorthand version of this is available by simply passing false as the only parameter:

  expect(subject.load(my_fixture.from_yaml(symbolize_names: false))).to eql({ 'foo' => 'bar' })
  expect(subject.load(my_fixture.from_json(symbolize_names: false))).to eql({ 'foo' => 'bar' })
  expect(subject.load(my_fixture.from_yaml(false))).to eql({ 'foo' => 'bar' })
  expect(subject.load(my_fixture.from_json(false))).to eql({ 'foo' => 'bar' })

License

The gem is available as open source under the terms of the MIT License.

rspec-file_fixtures's People

Contributors

bobf avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.