ruby - Step-by-step processes and contexts in RSpec -


i'm trying bdd rspec , i'm having hard time scaling techniques past simple examples.

http://betterspecs.org/#contexts informs me should use 'context' method make expectations more simple describe. there 2 problems i'm having:

1) wrapping test in 'context' creates new scope, setup has done multiple times. haven't found way of using 'before' hooks make dry— can see repeated code below.

2) case see below step-by-step process, each step builds off next. first compositor instantiated, instruction added, instructions cleared. wouldn't big technical issue if 1) addressed, you'll notice context descriptions starting snowball, seems defeat purpose of 'context' method.

can recommend refactor bring set of tests in line best practices?

require 'spec_helper'  describe compositor    context 'when instantiated correct parameters'     renderer = usbteensyrenderer.new("/dev/tty.usbmodem54121", 9600)     comp = compositor.new(renderer, [0, 0, 255, 255])     'has bounding rectangle'       expect(comp.bounding_box).to eq([0, 0, 255, 255])     end     'has renderer'       expect(comp.renderer).to eq(renderer)     end     'has empty array of drawing instructions'       expect(comp.drawing_instructions).to eq([])     end   end    context 'when 1 drawing instruction added'     renderer = usbteensyrenderer.new("/dev/tty.usbmodem54121", 9600)     comp = compositor.new(renderer, [0, 0, 255, 255])     comp.add_instruction(line.new( twodpoint.new(20, 20), twodpoint.new(40, 40) ))     'has length of one'       expect(comp.drawing_instructions.length).to eq(1)     end     'has instruction of class line'       expect(comp.drawing_instructions[0].class).to eq(line)     end   end    context 'when 1 drawing instruction added , drawing instructions cleared'     renderer = usbteensyrenderer.new("/dev/tty.usbmodem54121", 9600)     comp = compositor.new(renderer, [0, 0, 255, 255])     comp.add_instruction(line.new( twodpoint.new(20, 20), twodpoint.new(40, 40) ))     comp.clear()     'has length of zero'       expect(comp.drawing_instructions.length).to eq(0)     end   end  end 

these should make specs quite tight:

move renderer , comp let calls @ beginning describe block. let not share state across it examples, reduce risk of unexpected behaviour. note lazy evaluated though, may have potential side effects. link

describe compositor   let(:renderer){ usbteensyrenderer.new("/dev/tty.usbmodem54121", 9600) }`   ... 

use before block inside each context encapsulate context dependent setup

context 'when 1 drawing instruction added'   before { comp.add_instruction(line.new( twodpoint.new(20, 20),twodpoint.new(40, 40) )) }   ... 

inline it expectation 1 liner tests. should reduce snowballing descriptions.

it { expect(comp.bounding_box).to eq([0, 0, 255, 255]) } 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -