Class: Dataset::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/dataset/base.rb

Overview

The superclass of your Dataset classes.

It is recommended that you create a dataset using the Dataset::Block method first, then grow into using classes as you recognize patterns in your test data creation. This will help you to keep simple things simple.

Direct Known Subclasses

Block

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.helper_methodsObject

:nodoc:



29
30
31
# File 'lib/dataset/base.rb', line 29

def helper_methods # :nodoc:
  @helper_methods
end

.helpers(&method_definitions) ⇒ Object

Allows a subclass to define helper methods that should be made available to instances of this dataset, to datasets that use this dataset, and to tests that use this dataset.

This feature is great for providing any kind of method that would help test the code around the data your dataset creates. Be careful, though, to keep from adding business logic to these methods! That belongs in your production code.



20
21
22
23
24
25
26
27
# File 'lib/dataset/base.rb', line 20

def helpers(&method_definitions)
  @helper_methods ||= begin
    mod = Module.new
    include mod
    mod
  end
  @helper_methods.module_eval &method_definitions
end

.used_datasetsObject

:nodoc:



62
63
64
# File 'lib/dataset/base.rb', line 62

def used_datasets # :nodoc:
  @used_datasets
end

.uses(*datasets) ⇒ Object

Allows a subsclass to declare which datasets it uses.

Dataset is designed to promote ‘design by composition’, rather than ‘design by inheritance’. You should not use class hiearchies to share data and code in your datasets. Instead, you can write something like this:

class PeopleDataset < Dataset::Base; end
class DepartmentsDataset < Dataset::Base; end
class OrganizationsDataset < Dataset::Base
  uses :people, :departments
end

When the OrganizationsDataset is loaded, it will have all the data from the datasets is uses, as well as all of the helper methods defined by those datasets.

When a dataset uses other datasets, and those datasets themselves use datasets, things will be loaded in the order of dependency you would expect:

C uses B
A uses C
B, C, A is the load order


58
59
60
# File 'lib/dataset/base.rb', line 58

def uses(*datasets)
  @used_datasets = datasets
end

Instance Method Details

#loadObject

Invoked once before a collection of tests is run. If you use a dataset in multiple test classes, it will be called once for each of them - remember that the database will be cleared at the beginning of running a ‘suite’ or ‘group’ of tests, unless you are using nested contexts (as in nested describe blocks in RSpec).

Override this method in your subclasses.



75
# File 'lib/dataset/base.rb', line 75

def load; end