Module: DRYSpec::Helpers
- Defined in:
- lib/dryspec/helpers.rb
Instance Method Summary collapse
-
#for_subject(variable, &block) ⇒ Object
A shortcut for a combination of ‘context` and `subject`.
-
#let_context(*args, &block) ⇒ Object
This allows us to simplify the case where we want to have a context which contains one or more ‘let` statements.
-
#subject_should_not_raise(*args) ⇒ Object
Allows you to simply specify that the subject should not raise an exception.
-
#subject_should_raise(*args) ⇒ Object
Allows you to simply specify that the subject should raise an exception Takes no arguments or arguments of an exception class, a string, or both.
Instance Method Details
#for_subject(variable, &block) ⇒ Object
A shortcut for a combination of ‘context` and `subject`
131 132 133 134 135 136 137 |
# File 'lib/dryspec/helpers.rb', line 131 def for_subject(variable, &block) context "subject: #{variable}" do subject { send(variable) } instance_eval(&block) end end |
#let_context(*args, &block) ⇒ Object
This allows us to simplify the case where we want to have a context which contains one or more ‘let` statements
Supports giving either a Hash or a String and a Hash as arguments In both cases the Hash will be used to define ‘let` statements When a String is specified that becomes the context description If String isn’t specified, Hash#inspect becomes the context description
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/dryspec/helpers.rb', line 30 def let_context(*args, &block) context_string, hash = case args.map(&:class) when [String, Hash] then ["#{args[0]} #{args[1]}", args[1]] when [Hash] then [args[0].inspect, args[0]] end context(context_string) do hash.each { |var, value| let(var) { value } } instance_eval(&block) end end |
#subject_should_not_raise(*args) ⇒ Object
Allows you to simply specify that the subject should not raise an exception. Takes no arguments or arguments of an exception class, a string, or both.
As with RSpec’s basic ‘not_to raise_error` matcher, if you give a specific error, other unexpected errors may be swallowed silently
94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/dryspec/helpers.rb', line 94 def subject_should_not_raise(*args) error, = args it_string = "subject should not raise #{error}" it_string += " (#{.inspect})" if it it_string do if error expect { subject }.not_to raise_error error, else expect { subject }.not_to raise_error end end end |
#subject_should_raise(*args) ⇒ Object
Allows you to simply specify that the subject should raise an exception Takes no arguments or arguments of an exception class, a string, or both.
As with RSpec’s basic ‘to raise_error` matcher, if you don’t give an error then the an unexpected error may cause your test to pass incorrectly
68 69 70 71 72 73 74 75 76 |
# File 'lib/dryspec/helpers.rb', line 68 def subject_should_raise(*args) error, = args it_string = "subject should raise #{error}" it_string += " (#{.inspect})" if it it_string do expect { subject }.to raise_error error, end end |