How to test "initialize" method using RSpec - ruby

github_asset.rb
# frozen_string_literal: true
require 'asset_ingester/helpers/project_details'
require 'active_model'
module AssetIngester
module Asset
class GithubAsset
include ActiveModel::Serializers::JSON
attr_reader :id, :name, :full_name, :description, :owner_name, :owner_url,
:owner_avatar_url, :url, :html_url, :artifact_id, :jiras, :asset_type
# Public: Initializes an instance of the GithubAsset class
#
# repo - A hash containing github repository details
# asset_type - A string representation of the asset type
def initialize(repo, asset_type)
#id = repo[:id]
#name = repo[:name]
#full_name = repo[:full_name]
#description = repo[:description]
#owner_name = repo.dig(:owner, :login)
#owner_url = repo.dig(:owner, :url)
#owner_avatar_url = repo.dig(:owner, :avatar_url)
#url = repo[:url]
#html_url = repo[:html_url]
#asset_type = asset_type
#artifact_id = repo[:artifact_id] if repo[:artifact_id] && !repo[:artifact_id].empty?
#jiras = repo[:jiras] if repo[:jiras] && !repo[:jiras].empty?
end
# Public: Defines the JSON serialization structure
#
# https://edgeguides.rubyonrails.org/active_model_basics.html#serialization
def attributes
{
'id' => #id,
'name' => #name,
'full_name' => #full_name,
'description' => #description,
'owner_name' => #owner_name,
'owner_url' => #owner_url,
'owner_avatar_url' => #owner_avatar_url,
'url' => #url,
'html_url' => #html_url,
'asset_type' => #asset_type,
'artifact_id' => #artifact_id,
'jiras' => #jiras
}.compact
end
end
end
end
github_asset_spec.rb
require 'asset_ingester/asset/github_asset'
RSpec.describe AssetIngester::Asset::GithubAsset, type: :api do
context "creating" do
let(:asset_type) {"node_package"}
let(:repo) do
[id: 131_690,
name: 'acm-care-management-js',
full_name: 'AcuteCaseManagementUI/acm-care-management-js',
owner_name: 'AcuteCaseManagementUI',
owner_url: 'https://github.cerner.com/api/v3/users/AcuteCaseManagementUI',
owner_avatar_url: 'https://avatars.github.cerner.com/u/4095?',
url: 'https://github.cerner.com/api/v3/repos/AcuteCaseManagementUI/acm-care-management-js',
html_url: 'https://github.cerner.com/AcuteCaseManagementUI/acm-care-management-js',
asset_type: 'node_package',
artifact_id: "",
jiras: [] ]
end
describe '::attributes' do
subject { AssetIngester::Asset::GithubAsset.attributes(repo, asset_type) }
it 'instantiates the class with 2 arguments' do
expect(subject).to be_an_instance_of(AssetIngester::Asset::GithubAsset)
end
it 'sets a to the first argument' do
expect(subject.repo).to eq(repo)
end
it 'sets b to the second argument' do
expect(subject.asset_type).to eq(asset_type)
end
end
end
end
This is how i tried testing the github_asset.rb file but however I'am receiving the following error while defining the subject
AssetIngester::Asset::GithubAsset creating ::attributes instantiates the class with 2 arguments
Failure/Error: subject { AssetIngester::Asset::GithubAsset.attributes(repo, asset_type) }
NoMethodError:
undefined method `attributes' for AssetIngester::Asset::GithubAsset:Class
Did you mean? attr_writer
I am green to RSpec testing and want to know how this can be done.

You get that NoMethodError because you are trying to call attributes as a class method:
subject { AssetIngester::Asset::GithubAsset.attributes(repo, asset_type) }
# ^^^^^^^^^^
although in your code, attributes is defined as an instance method.
But apart from that, you ask "how ruby initialize method can be tested" and apparently your test code is all about initialize and not about attributes, so let's start at the beginning:
describe '::attributes' do
# ...
end
You want to test initialize, so attributes should be initialize. And since initialize is an instance method (rather than a class method), :: should be #:
describe '#initialize' do
# ...
end
And your subject should be an instance of GithubAsset which is created via new1:
describe '#initialize' do
subject { AssetIngester::Asset::GithubAsset.new(repo, asset_type) }
# ...
end
With that in place, you can start writing your tests, e.g.:
describe '#initialize' do
subject { AssetIngester::Asset::GithubAsset.new(repo, asset_type) }
it 'sets the id attribute' do
expect(subject.id).to eq(131690)
end
end
1 In Ruby, you rarely invoke ::allocate and #initialize directly. So instead of:
obj = Array.allocate
obj.send(:initialize, 5) { |i| i ** 2 }
obj #=> [0, 1, 4, 9, 16]
you typically just call new and let it call allocate and initialize for you:
obj = Array.new(5) { |i| i ** 2 }
obj #=> [0, 1, 4, 9, 16]

Related

How to test WebSockets For Hanami?

Using the following:
Hanami cookbook websockets
IoT Saga - Part 3 - Websockets! Connecting LiteCable to Hanami
I've been able to add WebSockets to Hanami, however as this is for production code I want to add specs; but I can't find information on how to test WebSockets and Hanami using Rspec.
I've been able to find this for RoR but nothing non-Rails specific or Hanami Specific, I have asked on the Hanami Gitter but not gotten a response yet.
Is the TCR gem the only way? I would prefer something simpler but If I must how would I set it up for anycable-go via litecable.
How can I test WebSockets for Hanami using Rspec?
To get this working requires several moving parts, the first is the Socket simulator which simulates the receiving socket on the webserver:
Note: url_path should be customized to what works for your web socket specific endpoint
# frozen_string_literal: true
require 'puma'
require 'lite_cable/server'
require_relative 'sync_client'
class SocketSimulator
def initialize(x_site_id_header: nil)
#server_logs = []
#x_site_id_header = x_site_id_header
end
attr_accessor :server_logs
def client
return #client if #client
url_path = "/ws?connection_token=#{connection_token}"
#client = SyncClient.new("ws://127.0.0.1:3099#{url_path}", headers: headers, cookies: '')
end
def connection_token
#connection_token ||= SecureRandom.hex
end
def user
return #user if #user
email = "#{SecureRandom.hex}#mailinator.com"
password = SecureRandom.hex
#user = Fabricate.create :user, email: email, site_id: site_id, password: password
end
def start
#server = Puma::Server.new(
LiteCable::Server::Middleware.new(nil, connection_class: Api::Sockets::Connection),
Puma::Events.strings
).tap do |server|
server.add_tcp_listener '127.0.0.1', 3099
server.min_threads = 1
server.max_threads = 4
end
#server_thread = Thread.new { #server.run.join }
end
def teardown
#server&.stop(true)
#server_thread&.join
#server_logs.clear
end
def headers
{
'AUTHORIZATION' => "Bearer #{jwt}",
'X_HANAMI_DIRECT_BOOKINGS_SITE_ID' => #x_site_id_header || site_id
}
end
def site_id
#site_id ||= SecureRandom.hex
end
def jwt
#jwt ||= Interactors::Users::GenerateJwt.new(user, site_id).call.jwt
end
end
The next thing is the SyncClient which is a fake client you can use to actually connect to the simulated socket:
# frozen_string_literal: true
# Synchronous websocket client
# Copied and modified from https://github.com/palkan/litecable/blob/master/spec/support/sync_client.rb
class SyncClient
require 'websocket-client-simple'
require 'concurrent'
require 'socket'
WAIT_WHEN_EXPECTING_EVENT = 5
WAIT_WHEN_NOT_EXPECTING_EVENT = 0.5
attr_reader :pings
def initialize(url, headers: {}, cookies: '')
#messages = Queue.new
#closed = Concurrent::Event.new
#has_messages = Concurrent::Semaphore.new(0)
#pings = Concurrent::AtomicFixnum.new(0)
#open = Concurrent::Promise.new
#ws = set_up_web_socket(url, headers.merge('COOKIE' => cookies))
#open.wait!(WAIT_WHEN_EXPECTING_EVENT)
end
def ip
Socket.ip_address_list.detect(&:ipv4_private?).try(:ip_address)
end
def set_up_web_socket(url, headers)
WebSocket::Client::Simple.connect(
url,
headers: headers
) do |ws|
ws.on(:error, &method(:on_error))
ws.on(:open, &method(:on_open))
ws.on(:message, &method(:on_message))
ws.on(:close, &method(:on_close))
end
end
def on_error(event)
event = RuntimeError.new(event.message) unless event.is_a?(Exception)
if #open.pending?
#open.fail(event)
else
#messages << event
#has_messages.release
end
end
def on_open(_event = nil)
#open.set(true)
end
def on_message(event)
if event.type == :close
#closed.set
else
message = JSON.parse(event.data)
if message['type'] == 'ping'
#pings.increment
else
#messages << message
#has_messages.release
end
end
end
def on_close(_event = nil)
#closed.set
end
def read_message
#has_messages.try_acquire(1, WAIT_WHEN_EXPECTING_EVENT)
msg = #messages.pop(true)
raise msg if msg.is_a?(Exception)
msg
end
def read_messages(expected_size = 0)
list = []
loop do
list_is_smaller = list.size < expected_size ? WAIT_WHEN_EXPECTING_EVENT : WAIT_WHEN_NOT_EXPECTING_EVENT
break unless #has_messages.try_acquire(1, list_is_smaller)
msg = #messages.pop(true)
raise msg if msg.is_a?(Exception)
list << msg
end
list
end
def send_message(message)
#ws.send(JSON.generate(message))
end
def close
sleep WAIT_WHEN_NOT_EXPECTING_EVENT
raise "#{#messages.size} messages unprocessed" unless #messages.empty?
#ws.close
wait_for_close
end
def wait_for_close
#closed.wait(WAIT_WHEN_EXPECTING_EVENT)
end
def closed?
#closed.set?
end
end
The last part is a fake channel to test against:
# frozen_string_literal: true
class FakeChannel < Api::Sockets::ApplicationChannel
identifier :fake
def subscribed
logger.info "Can Reject? #{can_reject?}"
reject if can_reject?
logger.debug "Streaming from #{stream_location}"
stream_from stream_location
end
def unsubscribed
transmit message: 'Goodbye channel!'
end
def can_reject?
logger.info "PARAMS: #{params}"
params.fetch('value_to_check', 0) > 5
end
def foo
transmit('bar')
end
end
To use in specs:
# frozen_string_literal: true
require_relative '../../../websockets-test-utils/fake_channel'
require_relative '../../../websockets-test-utils/socket_simulator'
RSpec.describe Interactors::Channels::Broadcast, db_truncation: true do
subject(:interactor) { described_class.new(token: connection_token, loc: 'fake', message: message) }
let(:identifier) { { channel: 'fake' }.to_json }
let(:socket_simulator) { SocketSimulator.new }
let(:client) { socket_simulator.client }
let(:user) { socket_simulator.user }
let(:connection_token) { socket_simulator.connection_token }
let(:channel) { 'fake' }
let(:message) { 'woooooo' }
before do
socket_simulator.start
end
after do
socket_simulator.teardown
end
describe 'call' do
before do
client.send_message command: 'subscribe',
identifier: identifier
end
it 'broadcasts a message to the correct channel' do
expect(client.read_message).to eq('type' => 'welcome')
expect(client.read_message).to eq(
'identifier' => identifier,
'type' => 'confirm_subscription'
)
interactor.call
expect(client.read_message).to eq(
'identifier' => identifier,
'message' => message
)
end
context 'with other connection' do
let(:user2) { Fabricate.create :user }
let(:jwt) { Interactors::Users::GenerateJwt.new(user2, site_id).call.jwt }
let(:site_id) { socket_simulator.site_id }
let(:url_path) { "/ws?connection_token=#{SecureRandom.hex}" }
let(:client2) { SyncClient.new("ws://127.0.0.1:3099#{url_path}", headers: {}, cookies: '') }
before do
client2.send_message command: 'subscribe',
identifier: identifier
end
it "doesn't broadcast to connections that shouldn't get it" do
aggregate_failures 'broadcast!' do
expect(client2.read_message).to eq('type' => 'welcome')
expect(client2.read_message).to eq(
'identifier' => identifier,
'type' => 'confirm_subscription'
)
expect(client.read_message).to eq('type' => 'welcome')
expect(client.read_message).to eq(
'identifier' => identifier,
'type' => 'confirm_subscription'
)
interactor.call
sleep 1
expect(client.read_message).to eq(
'identifier' => identifier,
'message' => message
)
expect { client2.close }.not_to raise_exception
end
end
end
end
end

FactoryBot is not creating the associated models list in the after create callback

I have two factories as follows:
FactoryBot.define do
factory :proofread_document do
factory :proofread_document_with_paragraphs do
after(:create) {|instance| create_list(:paragraph, 5, proofread_document: instance) }
end
end
end
FactoryBot.define do
factory :paragraph do
level { 1 }
association :proofread_document
end
end
In my RSpec test:
describe '#number_of_paragraphs_for' do
let(:proofread_document) { create(:proofread_document_with_paragraphs)}
it 'returns the number of paragraphs for the given level' do
expect(proofread_document.number_of_paragraphs_for("level_1")).to eq(1)
end
end
The test fails because there are no paragraphs:
proofead_document.paragraphs
=> []
Why are the associated paragraph objects not being created?
Associations are not magically reloaded on existing instances. This is not due to FactoryBot, but to ActiveRecord itself.
# example with activerecord:
class Foo
has_many :bars
end
class Bar
belongs_to :foo
end
foo = Foo.first
foo.bars
# => []
3.times { Bar.create(foo: foo) }
foo.bars
# => []
foo.reload.bars
# => [<#Bar ...>, <#Bar ...>, <#Bar ...>]
So you just need to reload the record (or just the association)
after(:create) do |inst|
create_list(...)
inst.paragraphs.reload
# or inst.reload
end
I found the problem.
In my paragraphs model I had placed a default scope as follows:
default_scope :minimum_word_count, ->{ where(proofread_word_count: MINIMUM_LEVEL_DATA_WORD_COUNT..Float::INFINITY)}
This caused some issues as the paragraph I was saving in my tests had a word count too low for the parameters defined in this scope.
#P.Boro and #rewritten helped by getting me to re-check my models and scopes.

Ruby: many related objects with similiar method naming pattern. How to map them to standardized methods

I'm working on a program that receives responses from an API that represent 'songs' from a database. Those responses arrive in my program as Struct objects, and they are structured slightly differently depending on which table they were pulled from.
For instance, the song object pulled from the 'track' table looks like:
song_1 = <struct Song track_artist="Michael Jackson", track_title="Billie Jean">
And the song object returned from the 'license' table looks like:
song_2 = <struct Song license_artist="Michael Jackson", license_title="Billie Jean">
If I want to get the 'artist' from song_1, I'd call song_1.track_artist, and with song_2, song_2.license_artist. But this is problematic when running loops. I want to be able to call song.title on any of them and receive the title.
Right now, I'm putting each Struct through a 'Normalizer' object when I receive it. It uses a hash mapping to change the method name of each Struct; the mapping more or less looks like:
{ track_artist: artist,
track_title: title,
license_artist: artist,
license_title: title }
This seems like it might be overkill. What's the best way to go about this?
You could use method_missing for this
module Unifier
def method_missing(name, *args, &block)
meth = public_methods.find { |m| m[/_#{name}/] }
meth ? send(meth, *args, *block) : super
end
def respond_to_missing?(method_name, include_private = false)
public_methods.any? { |m| m[/_#{method_name}/] } || super
end
end
class A
include Unifier
attr_reader :artist_name
def initialize
#artist_name = 123
end
end
a = A.new
a.respond_to?(:name) # => true
a.name # => 123
a.respond_to?(:title) # => false
a.title # => undefined method `title' for #<A:0x007fb3f4054330 #artist_name=123> (NoMethodError)
Update
For you case it will be more complex and tricky.
If you can make changes to place, where this Struct objects are created, then just patch classes, generated from Struct
song_1_class = Struct.new(:track_artist, :track_title) do
include Unifier
end
song_1 = song_1_class.new('Michael Jackson', 'Billie Jean')
puts "#{song_1.artist} - #{song_1.title}"
# => Michael Jackson - Billie Jean
If you can work only with objects of that classes - you could patch it dynamically
# We get objects of licence_struct class
licence_struct = Struct.new(:license_artist, :license_title)
song_2 = licence_struct.new('Michael Jackson', 'Billie Jean')
song_3 = licence_struct.new('Michael Jackson', 'Black of White')
def process_song(song)
puts "Song #{song} patched - #{song.respond_to?(:artist)}"
"#{song.artist} - #{song.title}"
rescue NoMethodError => err
# If we don't have methods on our struct - patch it
# If after patching object still dont respond to our method - throw exception
patch_object_from_error(err) ? retry : raise(err)
end
def patch_object_from_error(error)
receiver = error.receiver
receiver.class.class_exec { include Unifier }
meth = error.message.match(/undefined method `(\S+)'/)[1].to_sym
receiver.respond_to?(meth)
end
puts process_song(song_2)
# => Song #<struct license_artist="Michael Jackson", license_title="Billie Jean"> patched - false
# after retry
# => Song #<struct license_artist="Michael Jackson", license_title="Billie Jean"> patched - true
# => Michael Jackson - Billie Jean
puts process_song(song_3)
# dont need retry - class already patched
# => Song #<struct license_artist="Michael Jackson", license_title="Black of White"> patched - true
# => Michael Jackson - Black of White

mongoid document to_json including all embedded documents without ':include' on each one

Given an arbitrary mongoid document how do i convert it to JSON and include any embedded structures without specifically including those structures in my to_json statement.
For example:
#!/usr/bin/env ruby
require 'mongoid'
require 'json'
require 'pp'
class Doc
include Mongoid::Document
include Mongoid::Timestamps
field :doc_specific_info , type: String
embeds_many :persons
end
class Person
include Mongoid::Document
field :role , type: String
field :full_name , type: String
embeds_many :addresses
embedded_in :Doc
end
class Address
include Mongoid::Document
field :full_address , type: String
end
doc = Doc.new
doc.doc_specific_info = "TestReport"
p = Person.new
p.role = 'buyer'
p.full_name = 'JOHN DOE'
doc.persons << p
a = Address.new
a.full_address = '1234 nowhere ville'
doc.persons.first.addresses << a
# THIS STATEMENT
pp JSON.parse(doc.to_json(:include => { :persons => { :include => :addresses } } ) )
# GIVES ME
# {"_id"=>"4ee0d30fab1b5c5743000001",
# "created_at"=>nil,
# "doc_specific_info"=>"TestReport",
# "updated_at"=>nil,
# "persons"=>
# [{"_id"=>"4ee0d30fab1b5c5743000002",
# "full_name"=>"JOHN DOE",
# "role"=>"buyer",
# "addresses"=>
# [{"_id"=>"4ee0d30fab1b5c5743000003",
# "full_address"=>"1234 nowhere ville"}]}]}
# THIS STATEMENT
pp JSON.parse(doc.to_json() )
# GIVES ME
# {"_id"=>"4ee0d2f8ab1b5c573f000001",
# "created_at"=>nil,
# "doc_specific_info"=>"TestReport",
# "updated_at"=>nil}
So what I want is a statement something like this:
# FOR A STATEMENT LIKE THIS
pp JSON.parse(doc.to_json( :everything } ) )
# TO GIVE ME THE COMPLETE DOCUMENT LIKE SO:
# {"_id"=>"4ee0d30fab1b5c5743000001",
# "created_at"=>nil,
# "doc_specific_info"=>"TestReport",
# "updated_at"=>nil,
# "persons"=>
# [{"_id"=>"4ee0d30fab1b5c5743000002",
# "full_name"=>"JOHN DOE",
# "role"=>"buyer",
# "addresses"=>
# [{"_id"=>"4ee0d30fab1b5c5743000003",
# "full_address"=>"1234 nowhere ville"}]}]}
Does such a statement exist? If not then is my only alternative recusing the structure of the document and producing the proper includes myself? If there is another way to visualize the whole document that would be better?
This was answered by rubish in the forum but he didn't post an answer so I am doing that.
The answer is to use "doc.as_document.as_json" which will give you the whole document.
pp doc.as_document.as_json
You can override the #to_json method in your document to add all include.
class Person
def to_json(*args)
super(args.merge({:include => { :persons => { :include => :addresses } } } )
end
end
Now you can have all by doing
person.to_json()
If you want return the complete with only :everything option you can do :
class Person
def to_json(*args)
if args[0] == :everything
super(args.merge({:include => { :persons => { :include => :addresses } } } )
else
super(args)
end
end
end

RSpec mock or stub super in a model

How do I test this tiny part of the module, with super? (superclass is action_dispatch-3.0.1 testing/integration...) The module is included within spec/requests to intercept post:
module ApiDoc
def post(path, parameters = nil, headers = nil)
super
document_request("post", path, parameters, headers) if ENV['API_DOC'] == "true"
end
...
end
I don't want it to run the ActionDispatch::Integration-whatever, but I don't know how to mock or stub super to unit test it.
The module is only used within specs, and will have 100% test coverage, which proves those kinds of metrics as useless. I need to unit test it.
An example, if needed, this is how I use the module ApiDoc
require 'spec_helper'
describe "Products API" do
include ApiDoc ############## <---- This is my module
context "POST product" do
before do
#hash = {:product => {:name => "Test Name 1", :description => "Some data for testing"}}
end
it "can be done with JSON" do
valid_json = #hash.to_json
############### the following 'post' is overriden by ApiDoc
post("/products.json",valid_json,
{"CONTENT_TYPE" => "application/json",
"HTTP_AUTHORIZATION" => ActionController::HttpAuthentication::Basic.encode_credentials("user", "secret")})
response.should be_success
end
end
end
You can check if the method is called on the 'super' class
ActionDispatch::Integration.any_instance.should_receive(:post)
Since ApiDock is only required for your tests you could also overwrite the post method with alias_method_chain:
ActionDispatch::Integration.instance_eval do
def post_with_apidoc(path, parameters = nil, headers = nil)
post_without_apidoc
if ENV['API_DOC'] == "true"
document_request("post", path, parameters, headers)
end
end
alias_method_chain :post, :apidoc
end
This is merely a supplement to the answer. This is how I ended up testing it
require 'spec_helper'
describe 'ApiDoc' do
include ApiDoc
it "should pass the post to super, ActionDispatch" do
#path = "path"
#parameters = {:param1 => "someparam"}
#headers = {:aheader => "someheaders"}
ActionDispatch::Integration::Session.any_instance.expects(:post).with(#path, #parameters, #headers)
post(#path, #parameters, #headers)
end
end
class DummySuper
def post(path, parameters=nil, headers=nil)
#How to verify this is called?
end
end
class Dummy < DummySuper
include ApiDoc
end
describe Dummy do
it "should call super" do
subject.expects(:enabled?).once.returns(true)
#how to expect super, the DummySuper.post ?
path = "path"
parameters = {:param1 => "someparam"}
headers = {:aheader => "someheaders"}
subject.expects(:document_request).with("post", path, parameters, headers)
subject.post(path, parameters, headers)
end
end
and the slightly modified ApiDoc.
module ApiDoc
def enabled?
ENV['API_DOC'] == "true"
end
def post(path, parameters = nil, headers = nil)
super
document_request("post", path, parameters, headers) if enabled?
end
private
def document_request(verb, path, parameters, headers)
...
end
end
I could verify the super.post in the first test, but I still can't figure out how to do just that with my Dummy class specs.

Resources