Last active
August 29, 2015 14:22
-
-
Save Sinetheta/781fc3a592b7fee56fd2 to your computer and use it in GitHub Desktop.
custom-matchers.coffee
This file contains 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
customMatchers = | |
toChange: (util, customEqualityTesters) -> | |
compare: (actual, expected) -> | |
before = expected() | |
actual() | |
after = expected() | |
result = | |
pass: !util.equals(before, after, customEqualityTesters) | |
if result.pass | |
result.message = "Expected result not to change, but went from #{before} to #{after}" | |
else | |
result.message = "Expected result to rails change from #{before} to #{after}, but it did not" | |
result | |
toChangeBy: (util, customEqualityTesters) -> | |
compare: (actual, expected, changeBy) -> | |
before = expected() | |
actual() | |
after = expected() | |
result = | |
pass: util.equals(before + changeBy, after, customEqualityTesters) | |
if result.pass | |
result.message = "Expected result not to change by #{changeBy}, but went from #{before} to #{after}" | |
else | |
result.message = "Expected result to change by #{changeBy}, from #{before} to #{before + changeBy}, but instead got #{after}" | |
result | |
toChangeFromTo: (util, customEqualityTesters) -> | |
compare: (actual, expected, changeFrom, changeTo) -> | |
before = expected() | |
actual() | |
after = expected() | |
result = | |
pass: util.equals(before, changeFrom, customEqualityTesters) and | |
util.equals(after, changeTo, customEqualityTesters) | |
if result.pass | |
result.message = "Expected result not to change from #{changeFrom} to #{changeTo}, but it did" | |
else | |
result.message = "Expected result to change from #{changeFrom} to #{changeTo}, but instead it went from #{before} to #{after}" | |
result | |
beforeEach -> | |
jasmine.addMatchers customMatchers |
This file contains 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
class Resource | |
... | |
@populate: (data) -> | |
for options in data | |
resource = @find(options.id) | |
if resource? | |
resource.update(options) | |
else | |
new this(options) |
This file contains 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
describe '@populate', -> | |
beforeEach -> | |
@data = [{id: 1, name: 'new1'}] | |
@subject = (data) -> | |
@Resource.populate(data) | |
describe 'with new resources', -> | |
it 'adds the new resources', -> | |
expect(=> @subject(@data)).toChangeBy((=> @Resource.count()), 1) | |
describe 'with existing resources', -> | |
beforeEach -> | |
@resource = new @Resource({id: 1, name: 'old1'}) | |
it 'does not add a new resource', -> | |
expect(=> @subject(@data)).not.toChange((=> @Resource.count())) | |
it 'updates the matching resource', -> | |
expect(=> @subject(@data)).toChangeFromTo((=> @resource.name), 'old1', 'new1') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment