模块:RSpec::Mocks::Syntax 私有
- 定义于
- lib/rspec/mocks/syntax.rb
概述
此模块是私有 API 的一部分。 尽可能避免使用此模块,因为它可能会在将来被移除或更改。
提供用于启用和禁用 rspec-mocks 提供的可用语法的 方法。
类方法摘要 收起
-
.default_should_syntax_host ⇒ Object 私有
确定像
should_receive
和stub
这样的方法添加的位置。 -
.disable_expect(syntax_host = ::RSpec::Mocks::ExampleMethods) ⇒ Object 私有
禁用 expect 语法(
expect(dbl).to receive
,allow(dbl).to receive
等)。 -
.disable_should(syntax_host = default_should_syntax_host) ⇒ Object 私有
禁用 should 语法(
dbl.stub
,dbl.should_receive
等)。 -
.enable_expect(syntax_host = ::RSpec::Mocks::ExampleMethods) ⇒ Object 私有
启用 expect 语法(
expect(dbl).to receive
,allow(dbl).to receive
等)。 -
.enable_should(syntax_host = default_should_syntax_host) ⇒ Object 私有
启用 should 语法(
dbl.stub
,dbl.should_receive
等)。 -
.expect_enabled?(syntax_host = ::RSpec::Mocks::ExampleMethods) ⇒ Boolean 私有
指示 expect 语法是否启用。
-
.should_enabled?(syntax_host = default_should_syntax_host) ⇒ Boolean 私有
指示 should 语法是否启用。
类方法详情
.default_should_syntax_host ⇒Object
此方法是私有 API 的一部分。 尽可能避免使用此方法,因为它可能会在将来被移除或更改。
确定像 should_receive
和 stub
这样的方法添加的位置。
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/rspec/mocks/syntax.rb', line 181 def self.default_should_syntax_host # JRuby 1.7.4 introduces a regression whereby `defined?(::BasicObject) => nil` # yet `BasicObject` still exists and patching onto ::Object breaks things # e.g. SimpleDelegator expectations won't work # # See: https://github.com/jruby/jruby/issues/814 if defined?(JRUBY_VERSION) && JRUBY_VERSION == '1.7.4' && RUBY_VERSION.to_f > 1.8 return ::BasicObject end # On 1.8.7, Object.ancestors.last == Kernel but # things blow up if we include `RSpec::Mocks::Methods` # into Kernel...not sure why. return Object unless defined?(::BasicObject) # MacRuby has BasicObject but it's not the root class. return Object unless Object.ancestors.last == ::BasicObject ::BasicObject end |
.disable_expect(syntax_host = ::RSpec::Mocks::ExampleMethods) ⇒Object
此方法是私有 API 的一部分。 尽可能避免使用此方法,因为它可能会在将来被移除或更改。
禁用 expect 语法(expect(dbl).to receive
, allow(dbl).to receive
等)。
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/rspec/mocks/syntax.rb', line 150 def self.disable_expect(syntax_host=::RSpec::Mocks::ExampleMethods) return unless expect_enabled?(syntax_host) syntax_host.class_exec do undef receive undef undef undef allow undef expect_any_instance_of undef allow_any_instance_of end RSpec::Mocks::ExampleMethods::ExpectHost.class_exec do undef expect end end |
.disable_should(syntax_host = default_should_syntax_host) ⇒Object
此方法是私有 API 的一部分。 尽可能避免使用此方法,因为它可能会在将来被移除或更改。
禁用 should 语法(dbl.stub
, dbl.should_receive
等)。
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/rspec/mocks/syntax.rb', line 89 def self.disable_should(syntax_host=default_should_syntax_host) return unless should_enabled?(syntax_host) syntax_host.class_exec do undef should_receive undef should_not_receive undef stub undef unstub undef stub_chain undef as_null_object undef null_object? undef end Class.class_exec do undef any_instance end end |
.enable_expect(syntax_host = ::RSpec::Mocks::ExampleMethods) ⇒Object
此方法是私有 API 的一部分。 尽可能避免使用此方法,因为它可能会在将来被移除或更改。
启用 expect 语法(expect(dbl).to receive
, allow(dbl).to receive
等)。
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/rspec/mocks/syntax.rb', line 110 def self.enable_expect(syntax_host=::RSpec::Mocks::ExampleMethods) return if expect_enabled?(syntax_host) syntax_host.class_exec do def receive(method_name, &block) Matchers::Receive.new(method_name, block) end def (, &_block) matcher = Matchers::ReceiveMessages.new() matcher.warn_about_block if block_given? matcher end def (*, &block) Matchers::ReceiveMessageChain.new(, &block) end def allow(target) AllowanceTarget.new(target) end def expect_any_instance_of(klass) AnyInstanceExpectationTarget.new(klass) end def allow_any_instance_of(klass) AnyInstanceAllowanceTarget.new(klass) end end RSpec::Mocks::ExampleMethods::ExpectHost.class_exec do def expect(target) ExpectationTarget.new(target) end end end |
.enable_should(syntax_host = default_should_syntax_host) ⇒Object
此方法是私有 API 的一部分。 尽可能避免使用此方法,因为它可能会在将来被移除或更改。
启用 should 语法(dbl.stub
, dbl.should_receive
等)。
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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 81 82 83 84 85 |
# File 'lib/rspec/mocks/syntax.rb', line 26 def self.enable_should(syntax_host=default_should_syntax_host) @warn_about_should = false if syntax_host == default_should_syntax_host return if should_enabled?(syntax_host) syntax_host.class_exec do def should_receive(, opts={}, &block) ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__) ::RSpec::Mocks.(self, , opts, &block) end def should_not_receive(, &block) ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__) ::RSpec::Mocks.(self, , {}, &block).never end def stub(, opts={}, &block) ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__) if ::Hash === .each { |, value| stub().and_return value } else ::RSpec::Mocks.(self, , opts, &block) end end def unstub() ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__, "`allow(...).to receive(...).and_call_original` or explicitly enable `:should`") ::RSpec::Mocks.space.proxy_for(self).remove_stub() end def stub_chain(*chain, &blk) ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__) ::RSpec::Mocks::StubChain.stub_chain_on(self, *chain, &blk) end def as_null_object ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__) @_null_object = true ::RSpec::Mocks.space.proxy_for(self).as_null_object end def null_object? ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__) defined?(@_null_object) end def (, *args, &block) ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__) ::RSpec::Mocks.space.proxy_for(self).(, *args, &block) end unless Class.respond_to? :any_instance Class.class_exec do def any_instance ::RSpec::Mocks::Syntax.warn_unless_should_configured(__method__) ::RSpec::Mocks.space.any_instance_proxy_for(self) end end end end end |
.expect_enabled?(syntax_host = ::RSpec::Mocks::ExampleMethods) ⇒Boolean
此方法是私有 API 的一部分。 尽可能避免使用此方法,因为它可能会在将来被移除或更改。
指示 expect 语法是否启用。
175 176 177 |
# File 'lib/rspec/mocks/syntax.rb', line 175 def self.expect_enabled?(syntax_host=::RSpec::Mocks::ExampleMethods) syntax_host.method_defined?(:allow) end |
.should_enabled?(syntax_host = default_should_syntax_host) ⇒Boolean
此方法是私有 API 的一部分。 尽可能避免使用此方法,因为它可能会在将来被移除或更改。
指示 should 语法是否启用。
169 170 171 |
# File 'lib/rspec/mocks/syntax.rb', line 169 def self.should_enabled?(syntax_host=default_should_syntax_host) syntax_host.method_defined?(:should_receive) end |