简体   繁体   English

工厂未定义 RSPEC 测试 Ruby

[英]Factory not defined for RSPEC test Ruby

I 'm trying to write an RSpec test for this service.我正在尝试为此服务编写RSpec测试。 The service below essentially just makes a call to some endpoint and then create data in the postgres DB.下面的服务本质上只是调用某个端点,然后在postgres数据库中创建数据。

class SomeService
  API_URL = "someEndPoint"
  def process
    get_reviews
  end

  def get_reviews
    conn = Faraday.new(url: "#{APIURL}/#{ENV["ID"]}/reviews?language=en&stars=5") do |faraday|
      faraday.headers["apikey"] = ENV["KEY"]
      faraday.adapter Faraday.default_adapter
    end
    response = conn.get
    imported_reviews = []
    results = JSON.parse(response.body)
    results["reviews"].each do |item|
      review = SomeModel.create(
        title: item["title"],
        body: item["text"],
        posted_at: item["createdAt"],
        link: "https://somelink.com/#{item["id"]}",
        display_name: item["consumer"]["displayName"]
      )
      imported_reviews << review
    end
    imported_reviews
  end
end

My model is just the boilerplate model shown as:我的 model 只是样板 model 所示:

class SomeModel < ApplicationRecord
end

This is my spec file some_service_spec.rb .这是我的规范文件some_service_spec.rb I've written plenty of tests in JEST but it's just so foreign to me in Ruby.我在 JEST 中写了很多测试,但在 Ruby 中对我来说太陌生了。

# frozen_string_literal: true

require 'rails_helper'

describe TrustpilotService do
  describe "#process" do
    context 'with valid API_KEY' do
      before :all do
        WebMock.allow_net_connect!
      end
      after :all do
        WebMock.disable_net_connect!(allow_localhost: true)
      end
      it "should import reviews to database" do
        key = "90e0dbc7160d4024a1f12bc24b3d1def" #fake key#
        some_review = create(title: 'Blah', body: 'Blah blah', posted_at: '2019-09-30T20:35:14Z', link: 'https://somelink.com/reviews/asdf123', display_name: 'Jane Doe')
        some_review.save!
        service = described_class.new
        expect{ service.process }.to change{ SomeModel.count }
      end
    end
  end
end

Edit added my factory for reference编辑添加了我的工厂以供参考

FactoryBot.define do
  factory :trustpilot_review do

  end
end

Gem File宝石文件

group :test do
  gem "capybara", "~> 3.29.0"
  gem 'database_cleaner'
  gem 'factory_bot_rails'
  gem 'rails-controller-testing'
  gem 'rspec-rails', '~> 3.8'
  gem 'rspec-snapshot', '~> 0.1.2'
  gem "shoulda-matchers"
  gem "simplecov", require: false
  gem 'timecop'
  gem "webdrivers"
  gem 'webmock'
end

I essentially was just trying to replicate another spec file that was testing a service that does sort of the same thing but I'm getting Factory not registered: as an error.我基本上只是试图复制另一个规范文件,该文件正在测试一个做同样事情的服务,但我得到Factory not registered:作为一个错误。 I created a factory but not entirely sure what to even put in it.我创建了一个工厂,但不完全确定要在里面放什么。 All of the info on RSPEC out there is kind of outdated.所有关于 RSPEC 的信息都已经过时了。 As you can tell, I'm pretty new to Ruby so any help is appreciated.如您所知,我对 Ruby 还很陌生,因此不胜感激。

If this is the way you are calling your factory, then you missed naming the factory:如果这是您调用工厂的方式,那么您错过了命名工厂:

 some_review = create(title: 'Blah', body: 'Blah blah', posted_at: '2019-09-30T20:35:14Z', link: 'https://somelink.com/reviews/asdf123', display_name: 'Jane Doe')

Try尝试

 some_review = create(:trustpilot_review, title: 'Blah', body: 'Blah blah', posted_at: '2019-09-30T20:35:14Z', link: 'https://somelink.com/reviews/asdf123', display_name: 'Jane Doe')

Also it seems to me that the title, link, etc, should mostly be in the factory definition which is the point of factories.在我看来,标题、链接等主要应该在工厂定义中,这是工厂的重点。

If you want to use factory name with different name of model name.如果您想使用与 model 名称不同的工厂名称。 then need to add class_name like below:然后需要添加 class_name,如下所示:

FactoryBot.define do
  factory :trustpilot_review, class: 'SomeModel' do

  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM