2016-07-03 3 views
2

레일즈에서 루비를 배우고 있지만, 스스로 해결할 수없는 문제가 있습니다. 어느 누구도 나를 도울 수 있습니까?rspec-rails + capybara NoMethodError : 정의되지 않은 메소드`create '

나는 실행하면

Failure/Error: @user = create(:user) NoMethodError: 
     undefined method `create' for #<RSpec::ExampleGroups::User::PropertiesShouldNotNil:0xb807450> 
    # ./spec/models/user_spec.rb:6:in `block (3 levels) in <top (required)>' 

Gemfile :

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' 
gem 'rails', '5.0.0.rc2' 
# Use mysql as the database for Active Record 
gem 'mysql2' 
# Use SCSS for stylesheets 
gem 'sass-rails', '~> 5.0' 
# Use Uglifier as compressor for JavaScript assets 
gem 'uglifier', '>= 1.3.0' 
# Use CoffeeScript for .coffee assets and views 
gem 'coffee-rails', '~> 4.1.0' 
# See https://github.com/rails/execjs#readme for more supported runtimes 
# gem 'therubyracer', platforms: :ruby 
# Use jquery as the JavaScript library 
gem 'jquery-rails', '~> 2.3.0' 
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks 
gem 'turbolinks' 
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder 
gem 'jbuilder', '~> 2.0' 
# bundle exec rake doc:rails generates the API under doc/api. 
gem 'sdoc', '~> 0.4.0', group: :doc 
gem 'alipay', '~> 0.10.0' #支付宝接口 
gem 'capistrano-rails', :group => :development 
gem 'capistrano-passenger', :group => :development 
# Use ActiveModel has_secure_password 
# gem 'bcrypt', '~> 3.1.7' 
# Use Unicorn as the app server 
# gem 'unicorn' 
# Use Capistrano for deployment 
# gem 'capistrano-rails', group: :development 
group :development, :test do 
    # Call 'byebug' anywhere in the code to stop execution and get a debugger console 
    gem 'byebug' 
    # Access an IRB console on exception pages or by using <%= console %> in views 
    #gem 'web-console', '~> 2.0' 
    # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring 
    gem 'spring' 
    gem 'rspec-rails' 
    gem "capybara" 
end 
group :development do 
    gem 'web-console', group: :development 
end 

사양/rails_helper.rb :

ENV["RAILS_ENV"] ||= 'test' 
require 'spec_helper' 
require File.expand_path("../../config/environment", __FILE__) 
require 'rspec/rails' 
require 'capybara/rails' 
require 'capybara/rspec' 

ActiveRecord::Migration.maintain_test_schema! 

RSpec.configure do |config| 

    config.fixture_path = "#{::Rails.root}/spec/fixtures" 

    config.use_transactional_fixtures = true 

    config.infer_spec_type_from_file_location! 
end 
번들 간부 RSpec에 사양/모델/user_spec.rb,이 오류가 발생했습니다

app/models/user.rb

,210

사양/모델/user_spec.rb :

require 'rails_helper' 

RSpec.describe User, type: :model do 
    context "properties should not nil" do 
    before do 
     @user = create(:user) 
    end 

    subject{ @user } 

    it { should respond_to(:name) } 
    it { should respond_to(:passwd) } 
    end 
end 

답변

0

당신이 전화를하려고하는 create 방법은 일반적으로 FactoryGirl 클래스에 정의되어 있기 때문에이 오류입니다. FactoryGirl은 사용자 정의 팩토리를 정의하여 응용 프로그램 특정 모델을 인스턴스화 할 수있는 루비 보석입니다.

레일스 앱에이 보석을 통합해야 할 수도 있으므로 factory_girl_rails gem을 사용하는 것이 좋습니다. 다음과 같이 간단하게, 당신의 Gemfile:test 그룹으로 보석 선언을 추가

group :development, :test do 
    # Here go other gem declarations... 
    gem 'factory_girl_rails' 
end 

프로젝트에서이 보석을 포함하면,이에 (당신의 모델 공장을 정의해야합니다 사례, User 모델) 테스트 인스턴스를 작성하거나 만들려는 방식을 지정합니다. FactoryGirl으로 공장을 정의하는 방법에 대해이 기본 documentation을 읽는 것이 좋습니다 (이 문서를 계속 읽었습니다).

사전에 팩토리는 일반적으로 spec/factories 폴더 (이 경우에는 RSpec을 사용하고 있음) 아래에 정의되어 있으며 공통된 구조를 가지고 있다고 말할 수 있습니다. 그것은 나를 랜덤 값을 생성 할 수 있기 때문에,

FactoryGirl.define do 
    factory :user do 
    # Here you assign values to the different attributes, like: 
    # username "username" 
    # password "pass" 
    # ... 
    end 
end 

내가 faker gem FactoryGirl에 많이 사용 :이 경우, User 공장은 사양/공장/user.rb 같은 파일에 정의 될 수와 같은 수 이름, 전화 번호, 이메일 등은 항상 다르지만 의미가 있습니다. 그것에 관심이있는 경우 해당 설명서를보십시오.

희망 사항은 문제를 해결하는 데 도움이 될 수 있습니다.

+0

다니엘 루이즈, 당신의 대답은 내 문제를 해결하는 데 도움을 주셔서 감사합니다. – Lorbin

관련 문제