Forum: RSpec Testing Generators

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
90e140f8553ecdec1b0d7dbf608a0561?d=identicon&s=25 Pau Cor (phylae)
on 2008-10-07 12:02
Hi,

I have started writing my own generators, and I was wondering if anyone
had any suggestions for testing them.

I know I could just run the generator in a dummy application; write my
tests; and then copy the tests back into the generator. But that seems
backwards.

Any ideas?

Thanks
944f769c99deff7aa8bc3b5b93830b7a?d=identicon&s=25 Scott Taylor (Guest)
on 2008-10-07 16:29
(Received via mailing list)
On Oct 7, 2008, at 6:02 AM, Pau Cor wrote:

> Any ideas?
Why don't you generate the specs when you generate the code?  Then,
after you've generated the code you tell if the specs are actually
working...

Scott
90e140f8553ecdec1b0d7dbf608a0561?d=identicon&s=25 Pau Cor (phylae)
on 2008-10-08 00:24
Scott Taylor wrote:
> Why don't you generate the specs when you generate the code?

That's what I thought I'd have to do. The reason I'm reluctant to do it
is that either I need to write the real code first (and not tests
first). Or I need to rerun my generator every time I write a new test
(or write the code to make the test pass).

It seems awkward, but I guess it isn't avoidable.

Thanks!
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2008-10-08 01:50
(Received via mailing list)
On Tue, Oct 7, 2008 at 5:24 PM, Pau Cor <lists@ruby-forum.com> wrote:
> Scott Taylor wrote:
>> Why don't you generate the specs when you generate the code?
>
> That's what I thought I'd have to do. The reason I'm reluctant to do it
> is that either I need to write the real code first (and not tests
> first). Or I need to rerun my generator every time I write a new test
> (or write the code to make the test pass).

If you know what code you're trying to generate, you could use the
"gold master" approach, where you write a file with the code you want
to produce (the "gold master") and use that as the source of expected
values created by the generator. Using regular expressions or tools
like Hpricot you can do this incrementally.

It's not necessarily easy :) But it does let you write tests first.

Cheers,
David
This topic is locked and can not be replied to.