max pleaner
max pleaner

Reputation: 26768

How do you run a RSpec::ExampleGroup which hasn't been written to file?

I open up a rails console and require rspec_rails. I want to write a test like the following and be able to call it.

RSpec.describe("foo") do
  it "bar" do
    expect(false).to be false
  end
end

When I enter this into my console I see that a RSpec::ExampleGroup::Foo is created. However, I don't know how to call it.

I'm basically trying to reuse some helper methods I wrote for my tests. For example, I wrote a helper method called create_user_and_login which sends HTTP requests to a running server and creates a new user. I could reuse this method when migrating data our old database, but the method calls expect because up to now, it was only called from RSpec::ExampleGroup using the standard RSpec CLI runner.

Upvotes: 3

Views: 101

Answers (1)

MilesStanfield
MilesStanfield

Reputation: 4639

Here's how you can write and call that test in your console

# in your terminal
rails c test

# this worked for me to get rspec working in my console
require 'rspec/rails'

my_test = RSpec.describe("foo") do
  it "bar" do
    expect(false).to be false
  end
end

# now run it
my_test.run
# => true

Upvotes: 9

Related Questions