{"record":{"id":"8d67fe5141150617","repo":"thoughtbot/shoulda-matchers","slug":"when-specifying-a-value-as-a-block-a-context-must","errorCode":null,"errorMessage":"When specifying a value as a block, a context must be specified beforehand, e.g., #{store.name}.in_context(context).to { ... }","messagePattern":"When specifying a value as a block, a context must be specified beforehand, e\\.g\\., #(.+?)\\.in_context\\(context\\)\\.to (.+?)","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/shoulda/matchers/action_controller/set_session_or_flash_matcher.rb","lineNumber":26,"sourceCode":"        end\n\n        def in_context(context)\n          @context = context\n          self\n        end\n\n        def [](key)\n          @key = key\n          self\n        end\n\n        def to(expected_value = nil, &block)\n          if block\n            unless context_set?\n              message = 'When specifying a value as a block, a context must be'\\\n                ' specified beforehand,'\\\n                \" e.g., #{store.name}.in_context(context).to { ... }\"\n              raise ArgumentError, message\n            end\n\n            @expected_value = context.instance_eval(&block)\n          else\n            @expected_value = expected_value\n          end\n\n          self\n        end\n\n        def description\n          \"should #{expectation_description}\"\n        end\n\n        def matches?(controller)\n          @controller = store.controller = controller\n          !store.empty? && key_matches? && expected_value_matches?\n        end","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/thoughtbot/shoulda-matchers/blob/79cdfbc326e97c9b43991b644434ca53c1f62c4c/lib/shoulda/matchers/action_controller/set_session_or_flash_matcher.rb#L8-L44","documentation":"Error \"When specifying a value as a block, a context must be specified beforehand, e.g., #{store.name}.in_context(context).to { ... }\" thrown in thoughtbot/shoulda-matchers.","triggerScenarios":"Thrown at lib/shoulda/matchers/action_controller/set_session_or_flash_matcher.rb:26 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"79cdfbc326e97c9b43991b644434ca53c1f62c4c","analyzedAt":"2026-08-23T09:43:24.200Z","schemaVersion":2},"datasetVersion":"2026-08-23T13:39:53.451Z"}