Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 名为Spec的模型类存在Rspec问题,没有预期的模型类方法_Ruby On Rails_Ruby_Rspec - Fatal编程技术网

Ruby on rails 名为Spec的模型类存在Rspec问题,没有预期的模型类方法

Ruby on rails 名为Spec的模型类存在Rspec问题,没有预期的模型类方法,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,因此,每次运行模型类规范的RSpec测试时,我都会遇到此错误: NoMethodError: undefined method `new' for Spec:Module /home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator/new_constructor.rb:9:in `new' /home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7

因此,每次运行模型类规范的RSpec测试时,我都会遇到此错误:

NoMethodError: undefined method `new' for Spec:Module
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator/new_constructor.rb:9:in `new'
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator.rb:14:in `send'
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator.rb:10:in `method_missing'
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator/invocation_tracker.rb:11:in `method_missing'
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator.rb:14:in `send'
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/decorator.rb:10:in `method_missing'
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/configuration.rb:19:in `block in initialize'
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/attribute_assigner.rb:48:in `instance_exec'
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/attribute_assigner.rb:48:in `build_class_instance'
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/attribute_assigner.rb:13:in `object'
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/evaluation.rb:12:in `object'
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/strategy/build.rb:9:in `result'
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/factory.rb:42:in `run'
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/factory_runner.rb:29:in `block in run'
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/factory_runner.rb:28:in `run'
/home/log/.rvm/gems/ruby-2.3.1/gems/factory_girl-4.7.0/lib/factory_girl/strategy_syntax_method_registrar.rb:20:in `block in define_singular_strategy_method'
./spec/models/spec_spec.rb:13:in `block (3 levels) in <top (required)>'
./spec/models/spec_spec.rb:16:in `block (3 levels) in <top (required)>'
-e:1:in `load'
-e:1:in `<main>'

正如您已经猜到的,您正在看到
Spec
类和类之间的名称空间冲突

这里有两种解决方法

  • 将类及其文件名重命名为Rails.root/models/the_spec.rb

    class TheSpec < ApplicationRecord
      belongs_to :scenario
    
      # ... your code
    end
    
    class TheSpec
  • 为类命名名称空间并将其移动到Rails.root/models/internal/spec.rb

    class Internal::Spec < ApplicationRecord
      belongs_to :scenario
    
      # ... your code
    end
    
    class内部::Spec

  • 您还需要编辑和移动/重命名您的
    spec\u spec.rb
    文件以匹配。

    正如您已经猜到的,您的
    spec
    类和

    这里有两种解决方法

  • 将类及其文件名重命名为Rails.root/models/the_spec.rb

    class TheSpec < ApplicationRecord
      belongs_to :scenario
    
      # ... your code
    end
    
    class TheSpec
  • 为类命名名称空间并将其移动到Rails.root/models/internal/spec.rb

    class Internal::Spec < ApplicationRecord
      belongs_to :scenario
    
      # ... your code
    end
    
    class内部::Spec

  • 您还需要编辑和移动/重命名您的
    spec\u spec.rb
    文件,以使其匹配。

    我建议您不要命名您的模型
    spec
    -它可能已经在Rails内部使用过。为它找到另一个名字,看看你的bug是否消失了……我建议你不要给你的模型命名
    Spec
    ——它可能已经在Rails内部使用过了。找到它的另一个名字,看看你的bug是否消失了。。。