开发者

How to define suites in rspec2

How do I define a suite in rspec2. Indeed, what is a suite?

I want to break my specs down into two suites and reset everything between running the two suites (clean out the test db, reset warden etc ..). I guess i can do this with a before(:suite) block.

Update: My understanding is that an example is something like:

 it "should be true" do
...
end

a group is something like:

 describe Model do
...
end

This unde开发者_JS百科rstanding seems to be validated by putting a few debug statements in my code. But how is a suite defined? Is it just all the specs in the spec folder? In which case is there a way to achieve what I'm trying to do?

(I'm really surprised that I can't find this by googling or in the answers to previous questions.)


As I see it, there is no thing like a suite in RSpec itself. I have digging around a little bit and found the following:

  • There is a post by someone called "My top 7 RSpec best practices" where you find under section 6 "Create several test suites to speed up your workflow". That explains how to split up your specs into "suites" in a Rakefile and calling them individually.
  • What you try to reach is explained in the book "The RSpec Book" by David Chelimsky. There in chapter 16.5 he explains Global Hooks (before, after, around, ...) where as scope the keyword :suite can be used. Meaning:

If :each, the block is run before each matching example. If :all, the block is run once per group, before any matching examples have been run. If :suite, the block is run once before any example groups have run.

So if you divide your specs in example groups, you may control by using eg. before(:suite) do ... what should be done per example group.


You can use separate describe calls and in those you can do before and after calls:

describe "some cool text for suite 1" do
  before do
    ...
  end

  it "some test in suite 1" do
    ...
  end

  ...more tests...

  after do
    ...
  end
end

describe "some cool text for suite 2" do
  ...same shape as above...
end

This will allow you to separate the "setup" and "teardown" for your set of related tests. You can then add a rake task to run spec test by suite using the

rspec spec/ -e"Suite 1"  # call this task spec:suite1 or something like that

I've not done the rake tasks part of this but I regularly define conceptual suites with this mechanism.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜