2016-07-03 34 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 
のexec RSpecの仕様/モデル/ user_spec.rbをバンドルし、私はこのエラーを得ました

app/models/user.rb

app/models/user.rb 
    has_many :recipients 
end 

仕様/モデル/ 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は、アプリケーション固有のモデルをインスタンス化するカスタムファクトリを定義できるRubyの宝石です。

この宝石をRailsアプリに統合する必要があるかもしれないので、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たくさんでfaker gemを使用

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

:この場合、あなたのUser工場は仕様/工場/ user.rbのようなファイルに定義されていることになるとのようなもの名前、電話番号、電子メールなどは常に異なるが理にかなっている。興味がある場合は、そのドキュメントを見てください。

希望すると、問題を解決するのに役立ちます。

+0

Daniel Ruizさん、ありがとうございました。私の問題を解決するのに役立ちます。 – Lorbin

関連する問題