ruby on rails - rspec كيفية تشغيل اختبار واحد؟
ruby-on-rails ruby-on-rails-3 (9)
لقد عثرت على جميع أنواع الارتباطات عبر الإنترنت ولكن لم يتم العثور على أي منها حاليًا وأظهر كيفية إجراء اختبار واحد.
لدي الملف التالي:
/spec/controllers/groups_controller_spec.rb
ما الأمر في المحطة التي أستخدمها لتشغيل هذه المواصفات فقط وفي أي جهاز يمكنني تشغيل الأمر؟
ملف جوهري:
# 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
ملف المواصفات
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
في القضبان 5 ،
استخدمت هذه الطريقة لتشغيل ملف اختبار واحد (جميع الاختبارات في ملف واحد)
rails test -n /TopicsControllerTest/ -v
يمكن استخدام اسم الفئة لمطابقة الملف المطلوب TopicsControllerTest
صف class TopicsControllerTest < ActionDispatch::IntegrationTest
انتاج :
إذا كنت تريد أن تقوم بتعديل regex لتتطابق مع طريقة اختبار مفردة \TopicsControllerTest#test_Should_delete\
rails test -n /TopicsControllerTest#test_Should_delete/ -v
apneadiving الإجابة هي طريقة نظيفة لحل هذا. ومع ذلك ، لدينا الآن طريقة جديدة في Rspec 3.3. يمكننا ببساطة تشغيل rspec spec/unit/baseball_spec.rb[#context:#it]
بدلاً من استخدام رقم سطر. مأخوذة من here:
يقدم RSpec 3.3 طريقة جديدة لتحديد الأمثلة [...]
على سبيل المثال ، هذا الأمر:
$ rspec spec/unit/baseball_spec.rb[1:2,1:4]
... ستقوم بتشغيل$ rspec spec/unit/baseball_spec.rb[1:2,1:4]
الثاني والرابع أو المجموعة المحددة ضمن المجموعة الأولى من المستوى الأعلى المحددة في المواصفات / الوحدة / baseball_spec.rb.
لذا بدلاً من القيام rspec spec/unit/baseball_spec.rb:42
حيث يكون (الاختبار في السطر 42) هو الاختبار الأول ، يمكننا ببساطة القيام بـ rspec spec/unit/baseball_spec.rb:[1:1]
أو rspec spec/unit/baseball_spec.rb:[1:1:1]
اعتمادًا على كيفية تداخل حالة الاختبار.
بالنسبة للنموذج ، سيتم تشغيل الحالة على السطر رقم 5 فقط
bundle exec rspec spec/models/user_spec.rb:5
لوحدة التحكم: سيتم تشغيل الحالة على السطر رقم 5 فقط
bundle exec rspec spec/controllers/users_controller_spec.rb:5
بالنسبة لنموذج الإشارة أو جهاز التحكم ، قم بإزالة رقم السطر من أعلى
لتشغيل الحالة على جميع الطرز
bundle exec rspec spec/models
لتشغيل القضية على كل وحدة تحكم
bundle exec rspec spec/controllers
لتشغيل جميع الحالات
bundle exec rspec
بدءًا من rspec 2 ، يمكنك استخدام ما يلي:
# 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
عادة ما أفعل:
rspec ./spec/controllers/groups_controller_spec.rb:42
حيث تمثل 42
خط الاختبار الذي أرغب في تشغيله.
EDIT1:
يمكنك أيضًا استخدام العلامات. انظر here .
تعديل 2:
محاولة:
bundle exec rspec ./spec/controllers/groups_controller_spec.rb:42
مع الخليع:
rake spec SPEC=path/to/spec.rb
(يذهب الائتمان لهذا الجواب . اصوت له.)
EDIT (بفضلcirosantilli): لتشغيل سيناريو محدد ضمن المواصفات ، يجب عليك توفير تطابق نمط regex يطابق الوصف.
rake spec SPEC=path/to/spec.rb \
SPEC_OPTS="-e \"should be successful and return 3 items\""
هناك العديد من الخيارات:
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
هناك جوهرة ' watchr ' والتي يمكن أن تتعقب أي تغيير في الكود في الملف وتشغيل الإختبار تلقائيا من أجلك
يمكنك فعل شيء كهذا:
rspec/spec/features/controller/spec_file_name.rb
rspec/spec/features/controller_name.rb #run all the specs in this controller