[Rspec-devel] Adaptation of rpsec

Keith Hodges Keith.Hodges at warwick.ac.uk
Sat May 6 06:44:26 EDT 2006


Hello,

I have been working on an adaptation of rpsec in javascript. My code may
be a bit ropey but I think idea has some merit:

I have included an example spec below.

I use the word "expect" or "dontWant" to start sentences. I think that
this may be more portable than the rspec approach since I didn't want to
add methods to all objects in javascript.

I love the ability to test more than one thing at once
expect( a,b,c ).toBe( 3 ).or().toBe(5)

Specifications may be nested and supply setUp/tearDown for their
children. This gives the spec a tree structure and allows fixtures to be
branched in order to specify alternative use paths.

Any feedback/comments/etc would be welcomed.

For code examples etc:
http://www.warwick.ac.uk/~tuspam/js_spec/

Keith

=======================

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<title>Test Something</title>
<script type="text/javascript"
src="include/InheritanceFramework.js"></script>
<script type="text/javascript" src="include/JSspec.js"></script>
</head>
<body>
<H1>Specification Testing in Javascript</H1>
<script>
context('defines the overall behaviour suite at the first level')
    specify('begin a specification')
        expect(1).toBe(1)
        dontWant(1).toBe(2)
    specified()
    specify('strings compared as strings')
        expect('2+2').toBe('2+2')
    specified()
    specify('demo multiple expectands and tests')
        expect( 2,1+1,0.5 + 1.5, (4/2) ).toBe( 2 )
        expect( [ 1, 1+1, 1+2 ] ).toBe( [1,2,3] )
    specified()
    specify('that items should be empty')
        expect( {} , [], "" ).toBeEmpty()
    specified()
    specify('inclusion demo')
        expect( [1,2,3] ).toInclude( 2,3 )
        expect( "hello world" ).toInclude( "hello" , 'world' )
        dontWant( "hello world" ).toInclude( "hellpo"  )
    specified()
    specify('regex demo')
        expect( "hello world" ).toMatch( /HE.../i , /Lo/i )
    specified()
    specify('toRaise behaviour')
///        expect( "blah" ).toRaise('Foo')
///        expect( "blah" ).eval().toRaise('Foo')
        expect( "blah" ).eval().toRaise('ReferenceError')
        expect( "blah"
).eval().toRaise('ReferenceError').or().toRaise('Foo')
    specified()
    specify('logical ops')
        a= 1;
        expect(a).toBe(1)
        expect(a).not().toBe(2)
        dontWant(a).toBe(2)
        expect(a).toBe(1).or().toBe(2)
        expect(a).toBe(2).or().toBe(1)
        expect(a).toBe(1).and(a*2).toBe(2)
        expect(a).not().toBe(2).or().toBe(1)
    specified()
    /*nesting specifications with alternative naming convention (define
is equiv to specify)
       F is a default fixture provided by the framework*/
    define('nested spec at 2nd level with cascaded setups').
        setUp= function() { F.a= 1 }
        define('specification at the 3rd level').
            setUp= function() { F.a = F.a + 2 }
            define('further nesting, now at 4th level' ).setUp=
function() { F.a = F.a + 4 }
                expect( F.a ).toBe( 3 )
                specify('specification at 5th level, 3 setUps cascaded')
                    expect( F.a ).toBe( 7 )
                specified()
            defined()
        defined()
        specify('specifications at 3rd level, one setUp only')
            expect( F.a ).toBe( 1 )
        specified()
    defined()
context.writeDisplay()
</script>
</body>






		
___________________________________________________________ 
Switch an email account to Yahoo! Mail, you could win FIFA World Cup tickets. http://uk.mail.yahoo.com



More information about the Rspec-devel mailing list