GithubHelp home page GithubHelp logo

rspec / rspec-core Goto Github PK

View Code? Open in Web Editor NEW
1.2K 53.0 762.0 11.77 MB

RSpec runner and formatters

Home Page: http://rspec.info

License: MIT License

Ruby 82.35% Shell 1.12% HTML 1.00% Gherkin 15.52%
rspec ruby

rspec-core's Introduction

rspec-core Build Status Code Climate

rspec-core provides the structure for writing executable examples of how your code should behave, and an rspec command with tools to constrain which examples get run and tailor the output.

Install

gem install rspec      # for rspec-core, rspec-expectations, rspec-mocks
gem install rspec-core # for rspec-core only
rspec --help

Want to run against the main branch? You'll need to include the dependent RSpec repos as well. Add the following to your Gemfile:

%w[rspec rspec-core rspec-expectations rspec-mocks rspec-support].each do |lib|
  gem lib, :git => "https://github.com/rspec/#{lib}.git", :branch => 'main'
end

Basic Structure

RSpec uses the words "describe" and "it" so we can express concepts like a conversation:

"Describe an order."
"It sums the prices of its line items."
RSpec.describe Order do
  it "sums the prices of its line items" do
    order = Order.new

    order.add_entry(LineItem.new(:item => Item.new(
      :price => Money.new(1.11, :USD)
    )))
    order.add_entry(LineItem.new(:item => Item.new(
      :price => Money.new(2.22, :USD),
      :quantity => 2
    )))

    expect(order.total).to eq(Money.new(5.55, :USD))
  end
end

The describe method creates an ExampleGroup. Within the block passed to describe you can declare examples using the it method.

Under the hood, an example group is a class in which the block passed to describe is evaluated. The blocks passed to it are evaluated in the context of an instance of that class.

Nested Groups

You can also declare nested groups using the describe or context methods:

RSpec.describe Order do
  context "with no items" do
    it "behaves one way" do
      # ...
    end
  end

  context "with one item" do
    it "behaves another way" do
      # ...
    end
  end
end

Nested groups are subclasses of the outer example group class, providing the inheritance semantics you'd want for free.

Aliases

You can declare example groups using either describe or context. For a top level example group, describe and context are available off of RSpec. For backwards compatibility, they are also available off of the main object and Module unless you disable monkey patching.

You can declare examples within a group using any of it, specify, or example.

Shared Examples and Contexts

Declare a shared example group using shared_examples, and then include it in any group using include_examples.

RSpec.shared_examples "collections" do |collection_class|
  it "is empty when first created" do
    expect(collection_class.new).to be_empty
  end
end

RSpec.describe Array do
  include_examples "collections", Array
end

RSpec.describe Hash do
  include_examples "collections", Hash
end

Nearly anything that can be declared within an example group can be declared within a shared example group. This includes before, after, and around hooks, let declarations, and nested groups/contexts.

You can also use the names shared_context and include_context. These are pretty much the same as shared_examples and include_examples, providing more accurate naming when you share hooks, let declarations, helper methods, etc, but no examples.

If you want to reuse shared examples or contexts across your RSpec suite you can define them in a stand alone *.rb files (spec/support/shared_examples/definition.rb for example). But you will have to manually require them (there is no autoloading of spec/support/ directory unless you set it up yourself).

Metadata

rspec-core stores a metadata hash with every example and group, which contains their descriptions, the locations at which they were declared, etc, etc. This hash powers many of rspec-core's features, including output formatters (which access descriptions and locations), and filtering before and after hooks.

Although you probably won't ever need this unless you are writing an extension, you can access it from an example like this:

it "does something" do |example|
  expect(example.metadata[:description]).to eq("does something")
end

described_class

When a class is passed to describe, you can access it from an example using the described_class method, which is a wrapper for example.metadata[:described_class].

RSpec.describe Widget do
  example do
    expect(described_class).to equal(Widget)
  end
end

This is useful in extensions or shared example groups in which the specific class is unknown. Taking the collections shared example group from above, we can clean it up a bit using described_class:

RSpec.shared_examples "collections" do
  it "is empty when first created" do
    expect(described_class.new).to be_empty
  end
end

RSpec.describe Array do
  include_examples "collections"
end

RSpec.describe Hash do
  include_examples "collections"
end

A Word on Scope

RSpec has two scopes:

  • Example Group: Example groups are defined by a describe or context block, which is eagerly evaluated when the spec file is loaded. The block is evaluated in the context of a subclass of RSpec::Core::ExampleGroup, or a subclass of the parent example group when you're nesting them.
  • Example: Examples -- typically defined by an it block -- and any other blocks with per-example semantics -- such as a before(:example) hook -- are evaluated in the context of an instance of the example group class to which the example belongs. Examples are not executed when the spec file is loaded; instead, RSpec waits to run any examples until all spec files have been loaded, at which point it can apply filtering, randomization, etc.

To make this more concrete, consider this code snippet:

RSpec.describe "Using an array as a stack" do
  def build_stack
    []
  end

  before(:example) do
    @stack = build_stack
  end

  it 'is initially empty' do
    expect(@stack).to be_empty
  end

  context "after an item has been pushed" do
    before(:example) do
      @stack.push :item
    end

    it 'allows the pushed item to be popped' do
      expect(@stack.pop).to eq(:item)
    end
  end
end

Under the covers, this is (roughly) equivalent to:

class UsingAnArrayAsAStack < RSpec::Core::ExampleGroup
  def build_stack
    []
  end

  def before_example_1
    @stack = build_stack
  end

  def it_is_initially_empty
    expect(@stack).to be_empty
  end

  class AfterAnItemHasBeenPushed < self
    def before_example_2
      @stack.push :item
    end

    def it_allows_the_pushed_item_to_be_popped
      expect(@stack.pop).to eq(:item)
    end
  end
end

To run these examples, RSpec would (roughly) do the following:

example_1 = UsingAnArrayAsAStack.new
example_1.before_example_1
example_1.it_is_initially_empty

example_2 = UsingAnArrayAsAStack::AfterAnItemHasBeenPushed.new
example_2.before_example_1
example_2.before_example_2
example_2.it_allows_the_pushed_item_to_be_popped

The rspec Command

When you install the rspec-core gem, it installs the rspec executable, which you'll use to run rspec. The rspec command comes with many useful options. Run rspec --help to see the complete list.

Store Command Line Options .rspec

You can store command line options in a .rspec file in the project's root directory, and the rspec command will read them as though you typed them on the command line.

Get Started

Start with a simple example of behavior you expect from your system. Do this before you write any implementation code:

# in spec/calculator_spec.rb
RSpec.describe Calculator do
  describe '#add' do
    it 'returns the sum of its arguments' do
      expect(Calculator.new.add(1, 2)).to eq(3)
    end
  end
end

Run this with the rspec command, and watch it fail:

$ rspec spec/calculator_spec.rb
./spec/calculator_spec.rb:1: uninitialized constant Calculator

Address the failure by defining a skeleton of the Calculator class:

# in lib/calculator.rb
class Calculator
  def add(a, b)
  end
end

Be sure to require the implementation file in the spec:

# in spec/calculator_spec.rb
# - RSpec adds ./lib to the $LOAD_PATH
require "calculator"

Now run the spec again, and watch the expectation fail:

$ rspec spec/calculator_spec.rb
F

Failures:

  1) Calculator#add returns the sum of its arguments
     Failure/Error: expect(Calculator.new.add(1, 2)).to eq(3)

       expected: 3
            got: nil

       (compared using ==)
     # ./spec/calculator_spec.rb:6:in `block (3 levels) in <top (required)>'

Finished in 0.00131 seconds (files took 0.10968 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/calculator_spec.rb:5 # Calculator#add returns the sum of its arguments

Implement the simplest solution, by changing the definition of Calculator#add to:

def add(a, b)
  a + b
end

Now run the spec again, and watch it pass:

$ rspec spec/calculator_spec.rb
.

Finished in 0.000315 seconds
1 example, 0 failures

Use the documentation formatter to see the resulting spec:

$ rspec spec/calculator_spec.rb --format doc
Calculator
  #add
    returns the sum of its arguments

Finished in 0.000379 seconds
1 example, 0 failures

Contributing

Once you've set up the environment, you'll need to cd into the working directory of whichever repo you want to work in. From there you can run the specs and cucumber features, and make patches.

NOTE: You do not need to use rspec-dev to work on a specific RSpec repo. You can treat each RSpec repo as an independent project.

Also see

rspec-core's People

Contributors

alexch avatar alindeman avatar benhoskings avatar bennacer860 avatar benoittgt avatar bootstraponline avatar charlietanksley avatar cupakromer avatar dchelimsky avatar exviva avatar iamvery avatar ioquatix avatar jonrowe avatar justinko avatar kykyi avatar marcandre avatar matheusrich avatar moredip avatar mshytikov avatar myronmarston avatar nevinera avatar pat avatar phiggins avatar pirj avatar schnittchen avatar soulcutter avatar spicycode avatar urbanautomaton avatar xaviershay avatar yujinakayama avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rspec-core's Issues

have_selector( "tr>td", ... ) looks for <tr>td>MyString</tr>td>

I created a scaffold in Rails 3 beta 4 and I get an error on the generated view index test:

Failure/Error: rendered.should have_selector("tr>td", :content => "MyString".to_s, :count => 2)
expected following output to contain a td>MyStringtd> tag:

It should be looking for MyString and not td>MyStringtd>.

My Gemfile contains this:

gem 'rails', '3.0.0.beta4'
group :test do
gem 'capybara'
gem 'database_cleaner'
gem 'cucumber-rails'
gem 'cucumber'
gem "rspec-rails", ">= 2.0.0.beta.12"
gem "rspec", ">= 2.0.0.beta.12"
gem 'webrat'
gem 'spork'
gem 'launchy'
gem 'ZenTest'
gem 'autotest-rails'
gem 'autotest-growl'
end

Rake task should not shell out when it doesn't need to

rake spec currently shells out, which means double work loading up environments. It needs to shell out in certain situations (args to the ruby command, rcov, etc), but there are many in which it need not shell out. We should make it smarter, so it only shells out if necessary.

Set correct failed_results_re for autospec

The regexp given to Autotest for failed tests is wrong, and that causes Autotest to fire its "green" hook no matter the results of the test. I believe it also causes some more errors in Autotest's logic, but I'm not sure.

I believe the regexp is suppose to return a tuple of (class name, method name) for every test that fails. That's not possible with RSpec, as the individual tests have no name.

The offending file is, obviously, lib/autospec/rspec2.rb.

Can't load spec file

 %  rspec transactional_database_support_spec.rb 
/Users/sulky/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.0.0.beta.8/lib/rspec/core/configuration.rb:230:in `require': no such file to load -- transactional_database_support_spec.rb (LoadError)
        from /Users/sulky/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.0.0.beta.8/lib/rspec/core/configuration.rb:230:in `block in require_files_to_run'
        from /Users/sulky/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.0.0.beta.8/lib/rspec/core/configuration.rb:230:in `map'
        from /Users/sulky/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.0.0.beta.8/lib/rspec/core/configuration.rb:230:in `require_files_to_run'
        from /Users/sulky/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.0.0.beta.8/lib/rspec/core/runner.rb:42:in `configure'
        from /Users/sulky/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.0.0.beta.8/lib/rspec/core/runner.rb:28:in `run'
        from /Users/sulky/.rvm/gems/ruby-1.9.2-head/gems/rspec-core-2.0.0.beta.8/lib/rspec/core/runner.rb:12:in `block in autorun'

% ruby -v                    
ruby 1.9.2dev (2010-05-18 revision 27873) [x86_64-darwin10.2.0]

Ruby1.9.2: current_path( . ) is deleted from $LOAD_PATH
I fixed.
http://github.com/hasimo/rspec-core/commit/c8ce7716ccbac1b94353ed43d0ae50cface06225

rspec-core's cukes fail against jruby

rspec-2 seems to work w/ jruby, but there is some sort of load path problem running rspec-core-2's cukes against jruby (using rvm):

$ ruby -S cucumber features/command_line/example_name_option.feature:44
Using the default profile...

...

      expected: /1\ example,\ 0\ failures/,
           got: "\n----------------------------------------------------------------------\n/Users/dchelimsky/projects/rspec2/repos/rspec-core/lib/rspec/core.rb:46:in `require': no such file to load -- rspec/expectations (LoadError)\n\tfrom /Users/dchelimsky/projects/rspec2/repos/rspec-core/lib/rspec/core.rb:46\n\tfrom /Users/dchelimsky/projects/rspec2/repos/rspec-core/lib/rspec/core.rb:1:in `require'\n\tfrom /Users/dchelimsky/projects/rspec2/repos/rspec-core/lib/rspec/autorun.rb:1\n\tfrom /Users/dchelimsky/projects/rspec2/repos/rspec-core/lib/rspec/autorun.rb:2:in `require'\n\tfrom /Users/dchelimsky/projects/rspec2/repos/rspec-core/tmp/aruba/../../bin/rspec:2\n" (using =~) (Rspec::Expectations::ExpectationNotMetError)
      features/command_line/example_name_option.feature:46:in `Then I should see "1 example, 0 failures"'

Slightly extended backtrace by default

While writing code I just got this failure:

1) Rspec::Core::Runner ::DRbProxy with server running integrates via #run
    Failure/Error: Rspec::Core::Runner.new.run(%W[ --drb --drb-port #{drb_port} --version ], @err, @out)
    private method `puts' called for nil:NilClass
    # /Users/ashleymoran/Dropbox/PatchSpace/Development/work-source/rspec/rspec-core/spec/rspec/core/runner_spec.rb:180:in `block (4 levels) in <top (required)>'

Which is not enough to isolate the problem directly. Using -b on the command line gives me what I need, but too much. What would be ideal is to have the spec line that caused the failure, and also the topmost library code line too. In this case it would be:

1) Rspec::Core::Runner ::DRbProxy with server running integrates via #run
    Failure/Error: Rspec::Core::Runner.new.run(%W[ --drb --drb-port #{drb_port} --version ], @err, @out)
    private method `puts' called for nil:NilClass
    # /Users/ashleymoran/Dropbox/PatchSpace/Development/work-source/rspec/rspec-core/lib/rspec/core/runner.rb:104:in `run'
    # /Users/ashleymoran/Dropbox/PatchSpace/Development/work-source/rspec/rspec-core/spec/rspec/core/runner_spec.rb:180:in `block (4 levels) in <top (required)>'

(Merb handles this really well in its error pages, and lets you configure where you see errors from - lib, gems, framework. I'm sure you could go to town with error reporting, but just the above change would be really useful, to me at least.)

Rake fails with rcov warning for getc

If I comment out line 340 in rcov, it works:

Zlib::GzipReader.open(file){|gz| old_data = Marshal.load(gz) }

Stack trace below:

/Users/coreyhaines/.rvm/rubies/ruby-1.8.7-p249/bin/ruby -I "/Users/coreyhaines/.rvm/gems/ruby-1.8.7-p249/gems/cucumber-0.6.4/lib:lib" -S rcov -Ilib -Ispec --exclude "mocks,expectations,gems/,features,spec/ruby_forker,spec/rspec,spec/resources,spec/lib,spec/spec_helper.rb,db/,/Library/Ruby/,config/" --text-report --sort coverage --aggregate coverage.data "/Users/coreyhaines/.rvm/gems/ruby-1.8.7-p249/gems/cucumber-0.6.4/bin/cucumber" -- --format progress
/Users/coreyhaines/.rvm/gems/ruby-1.8.7-p249/gems/rcov-0.9.8/bin/rcov:340: warning: getc is obsolete; use STDIN.getc instead

Helper specs

Helper specs get generated, but they are not really "helper specs" in that they offer no specific support yet for spec'ing helpers.

They need to:

  • provide a helper object which, in rspec-rails-1, is an instance of ActionView::Base
  • make sure the helper object has access to all of the rails helpers (link_to, url_for, etc)
  • make sure the helper object includes the helper being spec'd, so its methods can be accessed

--debug not working RSpec (2.0.0.beta.7)

Running 'rake spec' with '--debug' in my .rspec file will produce:

"debugger statement ignored, use -d or --debug option on rspec to enable debugging"

Same thing when running 'rspec -d ./spec'

Running 'rdebug rspec ./spec' works ok.

Thanks!

Command line is missing --require directive

Hey, wondering if this was removed on purpose or if it was an oversight in the conversion to Rspec 2. Seems that some IDEs use the --require directive to load custom formatters to display progress in their own UI.

I can submit a patch if it should be implemented.

line number option only works on declaration line

Given a file like this:

describe "something" do
  it "does something" do
    # ....
  end
end

... you specify lines 1 or 2 and the example will run, but not if you specify line 3. In rspec-1 you can specify any line in the example.

describe and it let you overwrite important metadata

describe "something" do
  it "does something" do
    running_example.metadata[:description].should eq("does something")
  end
  it "does something else", :description => "foo" do
    running_example.metadata[:description].should eq("does something else")
  end
end

The latter example fails with:

    expected "does something else"
    got "foo"

This is bad news. We should disallow passing keys that have internal meaning to describe() and it().

access metadata from before(:all)

As of 2.0.0.beta.2, you can access the running example's metadata hash from an example or before/after(:each). Let's support accessing it from before/after(:all) as well - at least at the example group level. e.g.

describe "something" do
  before(:all) do
    running_example_group.metadata[:foo] = 'bar'
  end
  it "does something" do
    running_example.metadata[:example_group][:foo].should eq('bar")
    # or
    running_example_group.metadata[:foo].should eq("bar")
  end
end

drb support

Add a --drb command line switch with an option for specifying port.

Rake cucumber segmentation fault

I'm on leopard

If I comment out line 340 in rcov, then it works:

Zlib::GzipReader.open(file){|gz| old_data = Marshal.load(gz) }

stack trace below:

[coreyhaines: rspec-core (master)]$ rake cucumber
(in /Users/coreyhaines/Documents/projects/rspec2/rspec-dev/repos/rspec-core)
/Users/coreyhaines/.rvm/rubies/ruby-1.8.7-p249/bin/ruby -I "/Users/coreyhaines/.rvm/gems/ruby-1.8.7-p249/gems/cucumber-0.6.4/lib:lib" -S rcov -Ilib -Ispec --exclude "mocks,expectations,gems/,features,spec/ruby_forker,spec/rspec,spec/resources,spec/lib,spec/spec_helper.rb,db/,/Library/Ruby/,config/" --text-report --sort coverage --aggregate coverage.data "/Users/coreyhaines/.rvm/gems/ruby-1.8.7-p249/gems/cucumber-0.6.4/bin/cucumber" -- --format progress
/Users/coreyhaines/.rvm/gems/ruby-1.8.7-p249/gems/rcov-0.9.8/bin/rcov:340: [BUG] Segmentation fault
ruby 1.8.7 (2010-01-10 patchlevel 249) [i686-darwin9.8.0]

add pending method for use within an example

None of these work in rspec-2, but they all do in rspec-1:

describe "something" do
  it "does something" do
    pending
    # ....
  end

  it "does something else" do
    pending "getting something fixed"
    # ...
  end

  it "does something else" do
    pending "getting something fixed" do
      # ...
    end
  end

generate configuration for autotest

Add an option to the rspec command to configure a project directory for autotest, and eliminate the autospec command. Something like:

rspec --configure autotest

This would generate autotest/discover.rb, which would tell autotest that this is an rspec project.

gitconfig style configuration

Add support for something like .rspecconfig or .rspecrc in the home and project directories, override-able on the command line.

Config::CONFIG['host_os'] in configuration.rb breaks autotest

In configuration.rb's color_enabled= method, the use of Config::CONFIG['host_os'] in place of RUBY_PLATFORM breaks autotest:

$ autotest
~/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/rspec-core-2.0.0.beta.12/lib/rspec/core/backward_compatibility.rb:26:in `const_missing': uninitialized constant RSpec::Core::Configuration::Config (NameError)
        from ~/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/rspec-expectations-2.0.0.beta.12/lib/rspec/expectations/backward_compatibility.rb:6:in `const_missing'
        from ~/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/rspec-core-2.0.0.beta.12/lib/rspec/core/configuration.rb:126:in `color_enabled='
        from ~/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/rspec-core-2.0.0.beta.12/lib/rspec/core/configuration_options.rb:19:in `block in configure'
        from ~/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/rspec-core-2.0.0.beta.12/lib/rspec/core/configuration_options.rb:18:in `each'
        from ~/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/rspec-core-2.0.0.beta.12/lib/rspec/core/configuration_options.rb:18:in `configure'
        from ~/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/rspec-core-2.0.0.beta.12/lib/rspec/core/command_line.rb:7:in `initialize'
        from ~/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/rspec-core-2.0.0.beta.12/lib/rspec/core/runner.rb:35:in `new'
        from ~/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/rspec-core-2.0.0.beta.12/lib/rspec/core/runner.rb:35:in `run_in_process'
        from ~/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/rspec-core-2.0.0.beta.12/lib/rspec/core/runner.rb:26:in `run'
        from ~/.rvm/gems/ruby-1.9.2-head@rails3beta/gems/rspec-core-2.0.0.beta.12/lib/rspec/core/runner.rb:14:in `block in autorun'

Adding require 'rbconfig' to configuration.rb fixes the problem, but I don't know if that's the best solution.

rdoc.info failure - .document file

Noticed that your documentation failed to build on rdoc.info (noticed a similar problem with the expectations project too). The reason is that your .document file incorrectly lists a README.rdoc file for inclusion, and that file doesn't actually exist. I'd suggest changing the line in .document to README.markdown and give it another shot at generating. Should fix it. If not, let me know and I'll be happy to help.

Rspec 2.0.0.beta.9 Doesn't Run Specs If First *_spec.rb Is Blank

I wrote a bunch in here and clicked the "bug" label, which then wiped it. Really, this shit only happens to me.

So, as the title suggests, if the first file, alphabetically, is blank... Rspec silently kills itself, leaving friends, family and (most importantly) me heart broken and saddened [at the loss of a few hours].

I'm good at whining, but thank you for creating Rspec.

Rakefile :spec Task

desc "Run Unit Specs Only"
Rspec::Core::RakeTask.new(:spec) do |spec|
  spec.pattern = "spec/myproj/**/*_spec.rb"
end

Command-line Example

mbp:myproj aitrus$ ls -1 spec/myproj/
a_menace_spec.rb
bazzam_spec.rb
kablooey_spec.rb
mbp:myproj aitrus$ rake spec
(in /Users/aitrus/Development/myproj)
mbp:myproj aitrus$ mv spec/myproj/a_menace_spec.rb.rb spec/myproj/zomg_spec.rb
mbp:myproj aitrus$ ls -1 spec/myproj/
bazzam_spec.rb
kablooey_spec.rb
zomg_spec.rb
mbp:myproj aitrus$ rake spec
(in /Users/aitrus/Development/myproj)
........................


Finished in 3.4 seconds
24 examples, 0 failures
mbp:myproj aitrus$ mv spec/myproj/zomg_spec.rb spec/myproj/da_middle_spec.rb
mbp:myproj aitrus$ ls -1 spec/myproj/
bazzam_spec.rb
da_middle_spec.rb
kablooey_spec.rb
mbp:myproj aitrus$ rake spec
(in /Users/aitrus/Development/myproj)
........................


Finished in 3.4 seconds
24 examples, 0 failures
mbp:myproj aitrus$ mv spec/myproj/da_middle_spec.rb spec/myproj/arrrgh_spec.rb
mbp:myproj aitrus$ ls -1 spec/myproj/
arrrgh_spec.rb
bazzam_spec.rb
kablooey_spec.rb
mbp:myproj aitrus$ rake spec
(in /Users/aitrus/Development/myproj)
mbp:myproj aitrus$ 

let me type just rspec

Given a ./spec directory, the rspec command could assume that it holds specs and run them without having to type "rspec spec".

before(:all) is not called for nested groups

I'm using rspec-core from master and I have following spec:

describe "foo" do
  before(:all) { do_sth }
  context "some" do
    it "should do barrr" do
      some should expectation
    end
  end
end

do_sth is never called. It started happening somewhere around 7th May.

Around filters not compatilble with including custom matchers in RSpec configure block

AroundProxy is passed a instance of Example while it looks as though it should
receive an instance of ExampleGroup.

The example_block instance evaled in Example#run is instance evaled by an
ExampleGroup instance if no around eachs are given. When around eachs are
given it should probably do the same.

With the present behaviour the following works OK:

require "rubygems"
require 'rspec_sequel_matchers'

Rspec.configure do |c|
  c.include RspecSequel::Matchers
end

describe Foo do
  it { should validate_integer :id }
end

But this does not:

require "rubygems"
require 'rspec_sequel_matchers'

Rspec.configure do |c|
  c.include RspecSequel::Matchers
end

describe Foo do
  around(:each) do |example|
    DB.transaction { example.run ; raise Sequel::Error::Rollback }
  end
  it { should validate_integer :id }
end

It complains about validate_integer being an undefined method for
RSpec::Core::Example.

I have created a patch that fixes this, but due to my (lack) of understanding
of rspec's internals it may not be appropriate. It is available at http://github.com/benarmston/rspec-core/commit/f57284298f3199c670c72538c46c7aa5f69d6875

I located the bug with beta11 and then updated to beta12 to fix where upon I
saw the deprecation notice. So, I'm not sure how much you might want this.

Regarding the deprecation: I was using the around filter to run the specs in a
transaction like so:

around(:each) do |example|

DB.transaction { example.run ; raise Sequel::Error::Rollback }

end

The database library, Sequel, only provides access to a transaction via a
block form. Meaning that this couldn't be implemented with a before block and
a seperate after block. Am I misunderstanding the deprecation notice, or is
this form of hook no longer supported?

For what its worth, the method of doing this with rspec 1 was:

class Spec::Example::ExampleGroup

def execute(_args, &block)
DB.transaction{super(_args, &block); raise Sequel::Error::Rollback}
end

end

As ExampleGroup now has all of its methods as class methods, this method of
overriding and calling super is unlikely to work.

Regards,
Ben Armston

support autotest

rspec-1 has autotest support in lib/autotest/rspec.rb. We need to add this to rspec-2

Explicit subject block should evaluate at instance level

It is not currently possible to use a helper method in both the subject block and an it/specify block. e.g.

describe "Foo" do
  def my_helper
    MyClass
  end

  subject { my_helper.new }
  specify { my_helper.should do_something }
end

This code gives undefined method `my_helper' for #Rspec::Core::ExampleGroup... as the subject block is being evaluated in the context of the ExampleGroup class.

If an explicit subject is specified, it should be evaluated in the context of the ExampleGroup instance (this was the case for rspec-1).

2.0.0.beta.13 - examples in inner group do not get run if example in outer group fails

This was introduced in beta 13. Given the following example:

describe "outer" do
  it "fails" do
    raise "fail"
  end

  describe "inner" do
    it "passes" do
    end
  end
end

The inner group's example will not be executed.

The good news is that if the example in the outer group passes, the one in the inner group runs, so there aren't really false positives at the suite level (i.e. if the outer one is failing, as soon as you get it passing the inner one is run, so any failures there would reveal themselves). Mostly this just becomes confusing when resolving an issue in an outer group exposes failing examples in the inner group.

accessing constants from another module within Examples doesn't work

When including module within ExampleGroup then constants from that module should be accessible directly without specifying module name:

module MyModule 
  MyConstant = 1 
  
  def my_method 
    2 
  end 
end 

describe "including modules" do 
  include MyModule 

  it "works" do 
    my_method.should == 2 
    MyModule::MyConstant.should == 1 
  end 

  it "doesn't work, but should" do 
    MyConstant.should == 1 
  end 
end 

Running this simple spec will produce one failure with this message:

1) 
NameError in 'including modules doesn't work' 
uninitialized constant MyConstant

But in regular Ruby everything works as expected:

class MyClass
  include MyModule

  def initialize
    p my_method
    p MyModule::MyConstant
    p MyConstant
  end
end

MyClass.new # produces 2 1 1

Add global 'around' hook to support wrapping examples in transactions

Current master (post-beta.13, pre-beta.13) supports an around hook in an example group, which allows you to do stuff like this:

describe Something do
  around do |example|
    Sequel::Model.db.transaction { example.run }
  end
end

We need to make this available globally as well. Something like:

RSpec.configure do |c|
  c.around do |example|
    Sequel::Model.db.transaction { example.run }
  end
end

ExampleGroup.its(:my_method) should create Example in which subject is modified

Calling subject inside the example_block should return a modified subject.

The first argument to its() should be a symbol representing a method name to be called on subject.

For example, the following should pass:

class Foo
  def bar; 'fizz'; end
end
describe Foo do
  its(:bar) { should == 'fizz' }
end

Accessing the subject in a before or after block should be on the implicit or explicit subject, NOT the modified subject. See https://rspec.lighthouseapp.com/projects/5645/tickets/975-update-to-subject-not-available-to-its

Access to arbitrary options metadata within "before" block

I'm trying to use Steak and Capybara to run some scenarios using a JavaScript-capable backend (Culerity) and others using the standard rack_test backend.

In Cucumber this was done using tags, but with Steak and RSpec 1.3 I'm told you can do something like this:

scenario 'foo', :js => true do
  # ... this one runs under culerity
end

scenario 'bar' do
  # ... this one runs under rack_test
end

This is enabled by this config:

Spec::Runner.configure do |config|    
  config.before :each do
    Capybara.current_driver = :culerity if options[:js]
  end

  config.after :each do
    Capybara.use_default_driver if options[:js]
  end
end

Under RSpec 2 this trick won't work; it seems that "options" isn't available in the scope of these before/after config blocks. I've been able to dig out the options via some very ugly "instance_variable_get" inspection:

RSpec.configure do |config|
  config.before :each do
    if self.running_example.instance_variable_get(:@options)[:js]
      Capybara.current_driver = :culerity
    end
  end

  config.after :each do
    if self.running_example.instance_variable_get(:@options)[:js]
      Capybara.use_default_driver
    end
  end
end

So this ticket is about getting access to the arbitrary metadata that can be passed in to "describe" and "it" blocks via the options hash.

Note that in the use case I'm talking about above, using the "filter_run" stuff won't give me what I want (that's for selecting a subset of the examples to run, but I want to run all of the examples, but switch Capybara drivers on the fly).

What do you think about providing access to the options via an accessor?

I've noticed that I can get at the options passed in to the RSpec::Core::Example subclass; ie:

it "should foo", :stuff => true do
  ...
end

But I can't seem to get at the stuff passed in to the RSpec::Core::ExampleGroup subclass; ie:

describe "foo", :thing => true do
  ...
end

In practice I don't really care too much about access to that metadata, because I want to switch drivers on a per-scenario basis (ie. at the "it/scenario" level). But if you agree to making the metadata available at that level, I guess it makes sense to make the metadata at the "describe/feature" level available too.

What do you think?

If I can get a "green light" on the idea I'm happy to put a patch together.

Cheers,
Wincent

-f option doesn't work with fully qualified formatter classes names

-f option doesn't work with fully qualified formatter classes names.

E.g. if my formatter named "Spec::Runner::Formatter::TeamcityFormatter" then on run-time I get exception like this:
/Users/romeo/work/git/rspec-dev/repos/rspec-core/lib/rspec/core/configuration.rb:172:in const_defined?': wrong constant name Spec::Runner::Formatter::TeamcityFormatter (NameError) from /Users/romeo/work/git/rspec-dev/repos/rspec-core/lib/rspec/core/configuration.rb:172:informatter='
from /Users/romeo/work/git/rspec-dev/repos/rspec-core/lib/rspec/core/configuration_options.rb:22:in send' from /Users/romeo/work/git/rspec-dev/repos/rspec-core/lib/rspec/core/configuration_options.rb:22:inconfigure'
from /Users/romeo/work/git/rspec-dev/repos/rspec-core/lib/rspec/core/configuration_options.rb:21:in each' from /Users/romeo/work/git/rspec-dev/repos/rspec-core/lib/rspec/core/configuration_options.rb:21:inconfigure'
from /Users/romeo/work/git/rspec-dev/repos/rspec-core/lib/rspec/core/command_line.rb:11:in initialize' from /Users/romeo/work/git/rspec-dev/repos/rspec-core/lib/rspec/core/runner.rb:46:innew'
from /Users/romeo/work/git/rspec-dev/repos/rspec-core/lib/rspec/core/runner.rb:46:in run_in_process' from /Users/romeo/work/git/rspec-dev/repos/rspec-core/lib/rspec/core/runner.rb:37:inrun'
from /Users/romeo/work/git/rspec-dev/repos/rspec-core/lib/rspec/core/runner.rb:22

Problem occurs because "Object.const_defined?(formatter_to_use)" understands only simple constants without namespace. I'll attach a patch soon.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.