Module: PlanMyStuff::TestHelpers

Defined in:
lib/plan_my_stuff/test_helpers.rb

Overview

Test support for apps that consume PlanMyStuff. Provides:

  • ‘PlanMyStuff.test_mode!` to stub all API calls

  • Factory-style builders: ‘build_issue`, `build_comment`, `build_project`

  • RSpec matchers: ‘expect_pms_issue_created`, `expect_pms_comment_created`, `expect_pms_item_moved`

Usage (in a consuming Rails app):

require 'plan_my_stuff/test_helpers'

RSpec.configure do |config|
  config.include PlanMyStuff::TestHelpers
end

Not the harness for PlanMyStuff’s own unit specs. Gem-internal specs go through VCR cassettes against a real GitHub sandbox (see ‘spec/support/vcr.rb` and the `:pms_vcr_configured` shared context). Calling `PlanMyStuff.test_mode!` inside the gem would monkey-patch the very class methods the cassettes were recorded against, defeating the VCR coverage. The builder utilities (`build_issue`, `build_comment`, `build_project`, `stub_approvals`) are safe to use inside the gem as plain factories - they don’t replace any methods.

Defined Under Namespace

Modules: Notifications

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_comment(body: 'Test comment', visibility: :public, id: 1, issue: nil, metadata: {}) ⇒ PlanMyStuff::Comment

Builds a fake persisted Comment without hitting the API.

Parameters:

  • body (String) (defaults to: 'Test comment')
  • visibility (Symbol, String) (defaults to: :public)
  • id (Integer) (defaults to: 1)
  • issue (PlanMyStuff::Issue, nil) (defaults to: nil)

    parent issue (auto-built if nil)

  • metadata (Hash) (defaults to: {})

    raw metadata fields to merge

Returns:



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/plan_my_stuff/test_helpers.rb', line 92

def build_comment(body: 'Test comment', visibility: :public, id: 1, issue: nil, metadata: {})
  issue ||= build_issue

  comment = PlanMyStuff::Comment.new(
    body: body,
    id: id,
    issue: issue,
  )
  comment.visibility = visibility.to_sym

   = PlanMyStuff::CommentMetadata.from_hash({
    schema_version: PlanMyStuff::BaseMetadata::SCHEMA_VERSION,
    gem_version: PlanMyStuff::VERSION::STRING,
    visibility: visibility.to_s,
    custom_fields: {},
  }.merge())

  comment. = 
  comment.__send__(:persisted!)
  comment
end

.build_issue(title: 'Test Issue', body: 'Test body', state: :open, number: 1, repo: 'TestOrg/TestRepo', labels: [], metadata: {}) ⇒ PlanMyStuff::Issue

Builds a fake persisted Issue without hitting the API.

Parameters:

  • title (String) (defaults to: 'Test Issue')
  • body (String) (defaults to: 'Test body')
  • state (Symbol, String) (defaults to: :open)
  • number (Integer) (defaults to: 1)
  • repo (String) (defaults to: 'TestOrg/TestRepo')
  • labels (Array<String>) (defaults to: [])
  • metadata (Hash) (defaults to: {})

    raw metadata fields to merge

Returns:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/plan_my_stuff/test_helpers.rb', line 44

def build_issue(
  title: 'Test Issue',
  body: 'Test body',
  state: :open,
  number: 1,
  repo: 'TestOrg/TestRepo',
  labels: [],
  metadata: {}
)
  issue = PlanMyStuff::Issue.new(
    title: title,
    body: body,
    state: state.to_s,
    number: number,
    repo: repo,
    labels: labels,
  )

   = PlanMyStuff::IssueMetadata.from_hash({
    schema_version: PlanMyStuff::BaseMetadata::SCHEMA_VERSION,
    gem_version: PlanMyStuff::VERSION::STRING,
    visibility: 'public',
    custom_fields: {},
  }.merge())

  issue. = 
  issue.__send__(:persisted!)

  body_comment = build_comment(
    body: body,
    issue: issue,
    metadata: { issue_body: true },
  )
  issue.instance_variable_set(:@comments, [body_comment])

  issue
end

.build_project(title: 'Test Project', number: 1, statuses: [], items: []) ⇒ PlanMyStuff::Project

Builds a fake persisted Project without hitting the API.

Parameters:

  • title (String) (defaults to: 'Test Project')
  • number (Integer) (defaults to: 1)
  • statuses (Array<String>) (defaults to: [])

    status names (auto-assigned IDs)

  • items (Array<Hash>) (defaults to: [])

    item data hashes

Returns:



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/plan_my_stuff/test_helpers.rb', line 123

def build_project(title: 'Test Project', number: 1, statuses: [], items: [])
  status_options = statuses.each_with_index.map do |name, i|
    { id: "status_option_#{i}", name: name }
  end

  project = PlanMyStuff::Project.new(
    id: "PVT_fake_#{number}",
    number: number,
    title: title,
    statuses: status_options,
    fields: [{ id: 'field_status', name: 'Status', options: status_options }],
  )
  project.__send__(:persisted!)

  project.items = items.map do |item_hash|
    PlanMyStuff::ProjectItem.build(
      {
        id: item_hash[:id] || "PVTI_fake_#{rand(10_000)}",
        title: item_hash[:title] || 'Untitled',
        number: item_hash[:number],
        url: item_hash[:url],
        state: item_hash[:state],
        status: item_hash[:status],
        field_values: item_hash[:field_values] || {},
      },
      project: project,
    )
  end

  project
end

.extract_user_id(user) ⇒ Integer

Returns:

  • (Integer)


175
176
177
# File 'lib/plan_my_stuff/test_helpers.rb', line 175

def extract_user_id(user)
  user.is_a?(Integer) ? user : PlanMyStuff::UserResolver.user_id(user)
end

.stub_approvals(issue, approved: [], pending: []) ⇒ Array<PlanMyStuff::Approval>

Sets approvals on an in-memory Issue without hitting the API. Accepts user objects or integer user_ids.

Parameters:

  • issue (PlanMyStuff::Issue)
  • approved (Array<Object, Integer>) (defaults to: [])

    approvers who have approved

  • pending (Array<Object, Integer>) (defaults to: [])

    approvers who have not yet approved

Returns:



164
165
166
167
168
169
170
171
172
# File 'lib/plan_my_stuff/test_helpers.rb', line 164

def stub_approvals(issue, approved: [], pending: [])
  records =
    pending.map { |u| PlanMyStuff::Approval.new(user_id: extract_user_id(u), status: 'pending') } +
    approved.map do |u|
      PlanMyStuff::Approval.new(user_id: extract_user_id(u), status: 'approved', approved_at: Time.current)
    end
  issue..approvals = records
  records
end

Instance Method Details

#expect_pms_comment_created(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters (e.g. issue_number:, visibility:)



238
239
240
# File 'lib/plan_my_stuff/test_helpers.rb', line 238

def expect_pms_comment_created(**filters)
  expect_pms_action(:comment_created, 'comment to be created', **filters)
end

#expect_pms_comment_updated(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters (e.g. id:, body:)



248
249
250
# File 'lib/plan_my_stuff/test_helpers.rb', line 248

def expect_pms_comment_updated(**filters)
  expect_pms_action(:comment_updated, 'comment to be updated', **filters)
end

#expect_pms_comments_listed(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters (e.g. issue_number:, pms_only:)



243
244
245
# File 'lib/plan_my_stuff/test_helpers.rb', line 243

def expect_pms_comments_listed(**filters)
  expect_pms_action(:comments_listed, 'comments to be listed', **filters)
end

#expect_pms_issue_created(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters (e.g. repo:, title:)



208
209
210
# File 'lib/plan_my_stuff/test_helpers.rb', line 208

def expect_pms_issue_created(**filters)
  expect_pms_action(:issue_created, 'issue to be created', **filters)
end

#expect_pms_issue_found(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters (e.g. number:, repo:)



213
214
215
# File 'lib/plan_my_stuff/test_helpers.rb', line 213

def expect_pms_issue_found(**filters)
  expect_pms_action(:issue_found, 'issue to be found', **filters)
end

#expect_pms_issue_updated(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters (e.g. number:, state:)



223
224
225
# File 'lib/plan_my_stuff/test_helpers.rb', line 223

def expect_pms_issue_updated(**filters)
  expect_pms_action(:issue_updated, 'issue to be updated', **filters)
end

#expect_pms_issues_listed(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters



218
219
220
# File 'lib/plan_my_stuff/test_helpers.rb', line 218

def expect_pms_issues_listed(**filters)
  expect_pms_action(:issues_listed, 'issues to be listed', **filters)
end

#expect_pms_item_assigned(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters (e.g. project:, assignee:)



273
274
275
# File 'lib/plan_my_stuff/test_helpers.rb', line 273

def expect_pms_item_assigned(**filters)
  expect_pms_action(:item_assigned, 'item to be assigned', **filters)
end

#expect_pms_item_created(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters (e.g. project:, title:)



268
269
270
# File 'lib/plan_my_stuff/test_helpers.rb', line 268

def expect_pms_item_created(**filters)
  expect_pms_action(:item_created, 'item to be created', **filters)
end

#expect_pms_item_moved(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters (e.g. project:, status:)



263
264
265
# File 'lib/plan_my_stuff/test_helpers.rb', line 263

def expect_pms_item_moved(**filters)
  expect_pms_action(:item_moved, 'item to be moved', **filters)
end

#expect_pms_pipeline_deployment_completed(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters (e.g. item_id:, deployment_id:)



303
304
305
# File 'lib/plan_my_stuff/test_helpers.rb', line 303

def expect_pms_pipeline_deployment_completed(**filters)
  expect_pms_action(:pipeline_deployment_completed, 'pipeline deployment completed', **filters)
end

#expect_pms_pipeline_deployment_started(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters (e.g. project_number:, commit_sha:)



298
299
300
# File 'lib/plan_my_stuff/test_helpers.rb', line 298

def expect_pms_pipeline_deployment_started(**filters)
  expect_pms_action(:pipeline_deployment_started, 'pipeline deployment started', **filters)
end

#expect_pms_pipeline_in_review(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters (e.g. item_id:)



283
284
285
# File 'lib/plan_my_stuff/test_helpers.rb', line 283

def expect_pms_pipeline_in_review(**filters)
  expect_pms_action(:pipeline_in_review, 'pipeline item marked in review', **filters)
end

#expect_pms_pipeline_ready_for_release(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters (e.g. item_id:)



293
294
295
# File 'lib/plan_my_stuff/test_helpers.rb', line 293

def expect_pms_pipeline_ready_for_release(**filters)
  expect_pms_action(:pipeline_ready_for_release, 'pipeline item marked ready for release', **filters)
end

#expect_pms_pipeline_taken(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters (e.g. item_id:)



278
279
280
# File 'lib/plan_my_stuff/test_helpers.rb', line 278

def expect_pms_pipeline_taken(**filters)
  expect_pms_action(:pipeline_started, 'pipeline item to be taken', **filters)
end

#expect_pms_pipeline_testing(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters (e.g. item_id:)



288
289
290
# File 'lib/plan_my_stuff/test_helpers.rb', line 288

def expect_pms_pipeline_testing(**filters)
  expect_pms_action(:pipeline_testing, 'pipeline item moved to testing', **filters)
end

#expect_pms_project_found(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters (e.g. number:)



258
259
260
# File 'lib/plan_my_stuff/test_helpers.rb', line 258

def expect_pms_project_found(**filters)
  expect_pms_action(:project_found, 'project to be found', **filters)
end

#expect_pms_projects_listed(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters



253
254
255
# File 'lib/plan_my_stuff/test_helpers.rb', line 253

def expect_pms_projects_listed(**filters)
  expect_pms_action(:projects_listed, 'projects to be listed', **filters)
end

#expect_pms_viewers_added(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters (e.g. number:, user_ids:)



228
229
230
# File 'lib/plan_my_stuff/test_helpers.rb', line 228

def expect_pms_viewers_added(**filters)
  expect_pms_action(:viewers_added, 'viewers to be added', **filters)
end

#expect_pms_viewers_removed(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters (e.g. number:, user_ids:)



233
234
235
# File 'lib/plan_my_stuff/test_helpers.rb', line 233

def expect_pms_viewers_removed(**filters)
  expect_pms_action(:viewers_removed, 'viewers to be removed', **filters)
end