如何在上下文中嵌入上下文(已经在另一个上下文中)以在RSpec测试的共享示例中创建共享示例?

时间:2016-04-26 01:43:03

标签: ruby-on-rails rspec

我正在学习Ruby / Rails,并开始理解我的工作代码库并添加到测试中。我在理解如何设置共享示例时遇到了一些麻烦。在具有不同上下文的describe块中,我想在一个上下文中工作,在其中放置两个嵌套的上下文,它们将在其外部上下文中更改变量。

在下面的代码中,context 'and the attachment is a zipfile' do是我想要使用的描述块的上下文。我想通过为contentType和{具有两个不同的上下文来更改变量'application/x-zip-compressed' {1}}。

我可以设置两个内部内部上下文来更改contentType变量'application/octet-stream',还是不起作用,因为它是一个符号,我理解为一个不能改变的常量?

最初,这只是将let(:contentType) type哈希设置为"file1"。我正在探索一种使用共享示例的方法使类型变量,我可以根据上下文进行更改。

"applicatoin/x-zip-compressed"

1 个答案:

答案 0 :(得分:0)

是的,您可以设置在各种上下文中使用let定义的变量来获取各种行为。但是你的定义应该是后代上下文块到使用变量的地方。但要使用它it子句必须在变量definer上下文中声明:

describe 'the attachment is zipfile' do
  let(:encoded) { true }
  let(:data) { File.read( Rails.root + 'spec/fixtures/test_files/test.zip') }
  let(:expected_status) { 'new' }
  let(:attachments){{
    "file1" => {
      name: "test.zip",
      type: contentType,
      content: payload,
      base64: encoded
    }
  }}
  context 'and the attachment type is zip' do 
    let(:contentType) { 'application/x-zip-compressed' }

    it { $ request with compressed }
  end

  context 'and the attachment type is octet-stream' do 
    let(:contentType) { 'application/octet-stream' }

    it { # request with octet }
  end
end
相关问题