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
-
.build_comment(body: 'Test comment', visibility: :public, id: 1, issue: nil, metadata: {}) ⇒ PlanMyStuff::Comment
Builds a fake persisted Comment without hitting the API.
-
.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.
-
.build_project(title: 'Test Project', number: 1, statuses: [], items: []) ⇒ PlanMyStuff::Project
Builds a fake persisted Project without hitting the API.
- .extract_user_id(user) ⇒ Integer
-
.stub_approvals(issue, approved: [], pending: []) ⇒ Array<PlanMyStuff::Approval>
Sets approvals on an in-memory
Issuewithout hitting the API.
Instance Method Summary collapse
- #expect_pms_comment_created(**filters) ⇒ Object
- #expect_pms_comment_updated(**filters) ⇒ Object
- #expect_pms_comments_listed(**filters) ⇒ Object
- #expect_pms_issue_created(**filters) ⇒ Object
- #expect_pms_issue_found(**filters) ⇒ Object
- #expect_pms_issue_updated(**filters) ⇒ Object
- #expect_pms_issues_listed(**filters) ⇒ Object
- #expect_pms_item_assigned(**filters) ⇒ Object
- #expect_pms_item_created(**filters) ⇒ Object
- #expect_pms_item_moved(**filters) ⇒ Object
- #expect_pms_pipeline_deployment_completed(**filters) ⇒ Object
- #expect_pms_pipeline_deployment_started(**filters) ⇒ Object
- #expect_pms_pipeline_in_review(**filters) ⇒ Object
- #expect_pms_pipeline_ready_for_release(**filters) ⇒ Object
- #expect_pms_pipeline_taken(**filters) ⇒ Object
- #expect_pms_pipeline_testing(**filters) ⇒ Object
- #expect_pms_project_found(**filters) ⇒ Object
- #expect_pms_projects_listed(**filters) ⇒ Object
- #expect_pms_viewers_added(**filters) ⇒ Object
- #expect_pms_viewers_removed(**filters) ⇒ Object
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.
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.
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.
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: []) = 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: , fields: [{ id: 'field_status', name: '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
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.
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 |