Jak uruchomić pojedynczy test RSpec?

Mam następujący plik:

/spec/controllers/groups_controller_spec.rb

Jakiego polecenia w terminalu użyć do uruchomienia tylko tej specyfikacji i w jakim katalogu uruchomić to polecenie?

Mój plik gem:

# Test ENVIRONMENT GEMS
group :development, :test do
    gem "autotest"
    gem "rspec-rails", "~> 2.4"
    gem "cucumber-rails", ">=0.3.2"
    gem "webrat", ">=0.7.2"
    gem 'factory_girl_rails'
    gem 'email_spec'
end

Plik Spec:

require 'spec_helper'

describe GroupsController do
  include Devise::TestHelpers

  describe "GET yourgroups" do
    it "should be successful and return 3 items" do

      Rails.logger.info 'HAIL MARRY'

      get :yourgroups, :format => :json
      response.should be_success
      body = JSON.parse(response.body)
      body.should have(3).items # @user1 has 3 permissions to 3 groups
    end
  end
end
Author: fresskoma, 2011-05-25

12 answers

Zazwyczaj robię:

rspec ./spec/controllers/groups_controller_spec.rb:42

Gdzie 42 reprezentuje linię testu, który chcę uruchomić.

EDIT1:

Możesz również użyć tagów. Zobacz tutaj .

EDIT 2:

Try:

bundle exec rspec ./spec/controllers/groups_controller_spec.rb:42
 416
Author: apneadiving,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-17 23:59:49

Z Grabią:

rake spec SPEC=path/to/spec.rb

(kredyt idzie do ta ODPOWIEDŹ . Głosuj na niego.)

EDIT (dzięki @cirosantilli): aby uruchomić jeden konkretny scenariusz w specyfikacji, musisz podać dopasowanie wzorca regex pasujące do opisu.

rake spec SPEC=path/to/spec.rb \
          SPEC_OPTS="-e \"should be successful and return 3 items\""
 59
Author: Grant Birchmeier,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:54:50

Możesz przekazać regex do polecenia spec, które uruchomi tylko it bloki pasujące do podanej nazwy.

spec path/to/my_spec.rb -e "should be the correct answer"
 49
Author: Douglas F Shearer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-15 22:43:36

Moja preferowana metoda przeprowadzania konkretnych testów jest nieco inna - Dodałem wiersze

  RSpec.configure do |config|
    config.filter_run :focus => true
    config.run_all_when_everything_filtered = true
  end

Do mojego pliku spec_helper.

Teraz, kiedy chcę uruchomić jeden konkretny test (lub kontekst, lub spec), mogę po prostu dodać do niego tag "focus" i uruchomić mój test jak zwykle - tylko focused test(s) będzie działać. Jeśli usunę wszystkie znaczniki Fokusa, run_all_when_everything_filtered uruchomi się i uruchomi wszystkie testy normalnie.

Nie jest tak szybki i łatwy jak opcje wiersza poleceń-wymaga możesz edytować plik do testu, który chcesz uruchomić. Ale daje Ci to o wiele większą kontrolę.

 23
Author: GlyphGryph,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-10-21 17:47:48

Istnieje wiele opcji:

rspec spec                           # All specs
rspec spec/models                    # All specs in the models directory
rspec spec/models/a_model_spec.rb    # All specs in the some_model model spec
rspec spec/models/a_model_spec.rb:nn # Run the spec that includes line 'nn'
rspec -e"text from a test"           # Runs specs that match the text
rspec spec --tag focus               # Runs specs that have :focus => true
rspec spec --tag focus:special       # Run specs that have :focus => special
rspec spec --tag focus ~skip         # Run tests except those with :focus => true
 20
Author: Michael Durrant,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-05-14 00:17:14

@bezdech odpowiedź jest zgrabny sposób na rozwiązanie tego. Jednak teraz mamy nową metodę w Rspec 3.3. Możemy po prostu uruchomić rspec spec/unit/baseball_spec.rb[#context:#it] zamiast używać numeru linii. Zaczerpnięte z tutaj:

RSpec 3.3 wprowadza nowy sposób identyfikacji przykładów[...]

Na przykład polecenie:

$ rspec spec/unit/baseball_spec.rb[1:2,1:4] ... uruchomi 2. i 4. przykład lub grupę zdefiniowaną w 1. grupie najwyższego poziomu zdefiniowanej w spec / unit / baseball_spec.rb.

Więc zamiast robić rspec spec/unit/baseball_spec.rb:42 gdzie (test w linii 42) jest pierwszym testem, możemy po prostu zrobić rspec spec/unit/baseball_spec.rb[1:1] lub rspec spec/unit/baseball_spec.rb[1:1:1] w zależności od tego, jak zagnieżdżony jest przypadek testowy.

 5
Author: Ingo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-10 17:01:07

In rails 5,

Użyłem tego sposobu, aby uruchomić pojedynczy plik testowy (wszystkie testy w jednym pliku)

rails test -n /TopicsControllerTest/ -v

Nazwa klasy może być użyta do dopasowania do żądanego pliku TopicsControllerTest

Moja klasa class TopicsControllerTest < ActionDispatch::IntegrationTest

Wyjście:

Tutaj wpisz opis obrazka

Jeśli chcesz, możesz dostosować wyrażenia regularne, aby pasowały do pojedynczej metody testowej \TopicsControllerTest#test_Should_delete\

rails test -n /TopicsControllerTest#test_Should_delete/ -v
 3
Author: Alupotha,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-16 11:46:04

Począwszy od rspec 2 możesz użyć następującego:

# in spec/spec_helper.rb
RSpec.configure do |config|
  config.filter_run :focus => true
  config.run_all_when_everything_filtered = true
end

# in spec/any_spec.rb
describe "something" do
  it "does something", :focus => true do
    # ....
  end
end
 1
Author: Rudi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-04 19:54:02

Dla modelu, będzie uruchamiać case tylko na linii numer 5

bundle exec rspec spec/models/user_spec.rb:5

Dla kontrolera: uruchomi case tylko na linii numer 5

bundle exec rspec spec/controllers/users_controller_spec.rb:5

Dla modelu sygnału lub kontrolera usuń numer linii z góry

Aby uruchomić case na wszystkich modelach

bundle exec rspec spec/models

Aby uruchomić case na wszystkich kontrolerach

bundle exec rspec spec/controllers

Aby uruchomić wszystkie przypadki

 bundle exec rspec 
 1
Author: Sandeep Kapil,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-21 11:00:58

Biorąc pod uwagę, że jesteś w projekcie rails 3 z rspec 2, z głównego katalogu rails:

  bundle exec rspec spec/controllers/groups_controller_spec.rb 
Powinno zadziałać. miałem dość wpisywania tego, więc utworzyłem alias, aby skrócić 'bundle exec RSpec 'do'bersp'

'bundle exec' jest tak, że ładuje dokładnie środowisko gem określone w pliku gem: http://gembundler.com/

Rspec2 przełączył się z polecenia 'spec' na polecenie 'rspec'.

 0
Author: MissingHandle,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-24 20:58:42

Używam tego klejnotu do automatycznego uruchamiania testu. Wykonuje test po utworzeniu lub aktualizacji operacji na pliku testowym.

Https://github.com/guard/guard-test

Lub zwykle można uruchomić za pomocą następującego polecenia

RSpec spec / controllers / groups_controller_spec.rb

 0
Author: Rameshwar Vyevhare,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-02 12:15:44

Możesz zrobić coś takiego:

 rspec/spec/features/controller/spec_file_name.rb
 rspec/spec/features/controller_name.rb         #run all the specs in this controller
 0
Author: Prabhakar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-08 10:50:26