मैं रूबी में रूल्स एप्लिकेशन पर अपने मॉडलों के लिए Rspec पर परीक्षण लिख रहा हूं। और 'rspec कल्पना' शुरू करते समय मुझे यह त्रुटि मिली
command:
/spec/models/client_spec.rb:4:in `<top (required)>': uninitialized constant Client (NameError)
मैं रेल्स 4.0.0 और रूबी 2.0.0 का उपयोग करता हूं
यहाँ मेरा client_spec.rb है:
require 'spec_helper'
describe Client do
it 'is invalid without first_name', :focus => true do
client = Client.new
client.should_not be_valid
end
end
और जेमफाइल:
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.0.rc1'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0.rc1'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more:
gem 'turbolinks'
gem 'jbuilder', '~> 1.0.1'
group :development do
gem 'rspec-rails'
end
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', require: false
end
group :test do
gem 'rspec-rails'
gem 'factory_girl_rails'
gem 'database_cleaner'
end
और अंतिम ग्राहक पर। आरबी (आरओआर मॉडल और क्लास):
class Client < ActiveRecord::Base
has_many :cars
has_many :orders
has_one :client_status
has_one :discount_plan, through: :client_status
validates :email, format: { with: /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})\z/, :message => "Only emails allowed", :multiline => true }
validates :email, presence: true, if: "phone.nil?"
#validates :phone, presence: true, if: "email.nil?"
validates :last_name, :first_name, presence: true
validates :last_name, :first_name, length: {
minimum: 2,
maximum: 500,
wrong_length: "Invalid length",
too_long: "%{count} characters is the maximum allowed",
too_short: "must have at least %{count} characters"
}
end
अगर यह उपयोगी होगा मेरे spec_helper.rb फ़ाइल:
# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# Require this file using `require "spec_helper"` to ensure that it is only
# loaded once.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
config.run_all_when_everything_filtered = true
config.filter_run :focus
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = 'random'
#config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
--require spec_helper
से हटा रहा था.rspec
। जैसा कि यह पहले से ही निहित है--require rails_helper
, जिसे बाद में लोड किया जा रहा थाspec_helper
।