使用flexmock 模拟

配置 RSpec 以使用 flexmock,如下面的场景所示。

传递消息期望

给定一个名为“example_spec.rb”的文件,其中包含

RSpec.configure do |config|
  config.mock_with :flexmock
end

RSpec.describe "mocking with Flexmock" do
  it "passes when it should" do
    receiver = flexmock('receiver')
    receiver.should_receive(:message).once
    receiver.message
  end
end

我运行`rspec example_spec.rb`

那么示例应该全部通过。

失败的消息期望

给定一个名为“example_spec.rb”的文件,其中包含

RSpec.configure do |config|
  config.mock_with :flexmock
end

RSpec.describe "mocking with Flexmock" do
  it "fails when it should" do
    receiver = flexmock('receiver')
    receiver.should_receive(:message).once
  end
end

我运行`rspec example_spec.rb`

那么输出应该包含“1 个示例,1 个失败”。

在待处理示例中失败的消息期望(保持待处理)

给定一个名为“example_spec.rb”的文件,其中包含

RSpec.configure do |config|
  config.mock_with :flexmock
end

RSpec.describe "failed message expectation in a pending example" do
  it "is listed as pending" do
    pending
    receiver = flexmock('receiver')
    receiver.should_receive(:message).once
  end
end

我运行`rspec example_spec.rb`

那么输出应该包含“1 个示例,0 个失败,1 个待处理”。

并且退出状态应为 0。

在待处理示例中传递消息期望(失败)

给定一个名为“example_spec.rb”的文件,其中包含

RSpec.configure do |config|
  config.mock_with :flexmock
end

RSpec.describe "passing message expectation in a pending example" do
  it "fails with FIXED" do
    pending
    receiver = flexmock('receiver')
    receiver.should_receive(:message).once
    receiver.message
  end
end

我运行`rspec example_spec.rb`

那么输出应该包含“已修复”。

那么输出应该包含“1 个示例,1 个失败”。

并且退出状态应为 1。

访问`RSpec.configuration.mock_framework.framework_name`

给定一个名为“example_spec.rb”的文件,其中包含

RSpec.configure do |config|
  config.mock_with :flexmock
end

RSpec.describe "RSpec.configuration.mock_framework.framework_name" do
  it "returns :flexmock" do
    expect(RSpec.configuration.mock_framework.framework_name).to eq(:flexmock)
  end
end

我运行`rspec example_spec.rb`

那么示例应该全部通过。