In rspec, why can't I use be_false, etc. In helper classes?

When using the helper class with rspec test I don't see to use idiom .should be_false. This is normal in the function defined in the .rb helper file, but when it is inside the class, the character is be_falsenot found. Example below - why doesn’t this work? How can I use be_falseet al in an assistant?

It seems possible that he deliberately claims that such statements only work in tests. I have assistants who may fail due to, for example. network problems, which are actually unsuccessful testing attempts, because the network used by my assistants is part of the system under test. How can I make my tests fail gracefully inside the helper class?

results

$ spec ./test.rb 
helper_foo 1
helper_foo 2
helper_foo 3
FunctionFoo 1
F

1)
NameError in 'my_test should test that helper classes can use should be_false etc'
undefined local variable or method `be_false' for #<HelperFoo:0x2b265f7adc98>
./helper.rb:13:in `FunctionFoo'
./test.rb:13:

Finished in 0.004536 seconds

1 example, 1 failure

test.rb

require "helper.rb"

describe "my_test" do
    it "should test that helper classes can use should be_false etc" do

        (1 == 1).should be_true
        (2 == 1).should be_false

        helper_foo()

        instance = HelperFoo.new()
        instance.FunctionFoo
    end
  end

helper.rb

def helper_foo
    puts "helper_foo 1"
    (1==2).should be_false
    puts "helper_foo 2"
    (2==2).should be_true
    puts "helper_foo 3"
end

class HelperFoo
    def FunctionFoo
        puts "FunctionFoo 1"
        (1==2).should be_false
        puts "FunctionFoo 2"
        (2==2).should be_true
        puts "FunctionFoo 3"
    end
end
+3
source share
1 answer

Type expectations be_trueare only available in a block context it. When you call a method on HelperFoo.new, it no longer runs in the context of any magic class it.

You must read instance_evalto understand what constitutes an assessment context.

Also, if you need to define external RSpec examples, you should use the Common Example Groups .

+5
source

Source: https://habr.com/ru/post/1756596/


All Articles