Hello!
You’ve not really provided enough code to be able to help entirely, but for
certain I can say you have a problem in your spec.
By calling `after` you are never executing your `current_subject.console`
during your test. So the `expect(current_subject).to receive(:create)` will
always fail your test.
The fact that your test is exiting indicates that the allow is working fine and
its progressing through your console input, as otherwise that loop would never
exit and your test would hang.
Personally this is not how I would test this. Instead I would refactor the code
so that loop causes a step change to occur e.g:
```
def console
loop { process_state(@state) }
end
def process_state(state)
exit unless STATES.include state
public_send(state)
end
```
I would then test process_state in what ever transition you would want, this
avoids the loop. Just make sure you are calling the methods required in your
test and not in `after.
Cheers
Jon Rowe
---------------------------
[email protected]
jonrowe.co.uk
On 5 February 2020 at 13:51, Dmitry Sokolyansky wrote:
> I'm trying to test a "console" method that switches states. Each state has
> user input.
> I have test example on github https://github.com/dmitriy-sokolyanskiy/ref
> (https://github.com/dmitriy-sokolyanskiy/ref)
> Desired behavior: in test I expect the "console" method to be launched with
> initial `@state = :greeting` and switching to `@state = :base_option`, which
> asks for user input, for example, `"create"`, which switches to `state =
> :main_menu`, which requires user input, for example, an `“exit”` that leads
> to an exit.
>
> def
> console
> loop
> do
>
> break unless STATES.include?(@state)
>
>
> public_send (@state)
>
> end
>
> end
>
>
> def
> greeting
> message
> (:greeting)
>
> @state = :
> base_option
>
> end
>
>
>
> Problems:
>
> 1. it does not execute console input since `@states` does not change
> correctly.
> 2. `expect(current_subject).to receive(:create)` - is not met.
>
> let(:current_subject) { described_class.new }
> let(:input_sequence) { %w[create exit] }
> describe '#console' do
> context 'when correct method calling' do
> after do
> # @state=:greeting
> current_subject.
> console
>
> # @state=nil
> end
>
> it 'create account if input is create' do
> allow(current_subject).to receive_message_chain(:gets, :chomp) {
> input_sequence }
>
>
> expect
> (current_subject).to receive(:create)
>
> end
> end
> end
>
> I'm stuck. Goodle doesn't know about it too (
--
You received this message because you are subscribed to the Google Groups
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/rspec/dejalu-217-538e55c2-08f5-420f-8171-522340ce7101%40jonrowe.co.uk.