Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.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-on-rails-4/2.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/1/visual-studio-2008/2.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 如何模拟不存在的ActiveRecord模型来测试Rails可安装引擎_Ruby On Rails_Ruby On Rails 4_Rspec Rails - Fatal编程技术网

Ruby on rails 如何模拟不存在的ActiveRecord模型来测试Rails可安装引擎

Ruby on rails 如何模拟不存在的ActiveRecord模型来测试Rails可安装引擎,ruby-on-rails,ruby-on-rails-4,rspec-rails,Ruby On Rails,Ruby On Rails 4,Rspec Rails,我花了相当长的时间试图找到一个解决方案,如何模拟相关的ActiveRecord模型,但几乎一无所获 我下面将构建一个简单的可安装引擎。我很好奇在这种情况下如何进行测试,如何模拟只存在于未来应用程序中的相关ActiveRecord模型 例如,如何编写测试用例,使“post=post.create!valid_attribute”通过 在post.rb中: attr_accessor :author_name belongs_to :author, class_name: Blorgh.author

我花了相当长的时间试图找到一个解决方案,如何模拟相关的ActiveRecord模型,但几乎一无所获

我下面将构建一个简单的可安装引擎。我很好奇在这种情况下如何进行测试,如何模拟只存在于未来应用程序中的相关ActiveRecord模型

例如,如何编写测试用例,使“post=post.create!valid_attribute”通过

在post.rb中:

attr_accessor :author_name
belongs_to :author, class_name: Blorgh.author_class

before_save :set_author

private
  def set_author
    self.author = Blorgh.author_class.find_or_create_by(name: author_name)
  end
在测试用例中:

  it "assigns all posts as @posts" do
    //how to do here?
    post = Post.create! valid_attributes

    get :index, {use_route: :blorgh}, valid_session
    assigns(:posts).should eq([post])
  end
错误:

  Failure/Error: post = Post.create! valid_attributes
  NoMethodError:
  undefined method `find_or_create_by' for nil:NilClass
blorg\lib目录中的blorg.rb

  module Blorgh4mRspect
    mattr_accessor :author_class    
    def self.author_class
      @@author_class.constantize
    end
  end

您不想继续引用
Blorgh.author\u类
抽象而不是对
用户
类的硬编码引用吗

def set_author
  self.author = Blorgh.author_class.constantize.find_or_create_by(name: author_name)
end

哎呀,我抄错了,已经更正了。为了澄清我的问题,我还添加了一些上下文。