簡體   English   中英

如何在一個上下文中嵌套上下文(已經在另一個上下文中)以在RSpec測試的共享示例中創建一個共享示例?

[英]How to nest context within a context (already in another context) to make a shared example within a shared example for an RSpec test?

我正在學習Ruby / Rails,並從了解我的工作的代碼庫開始並添加了測試。 我在理解如何設置共享示例時遇到了一些麻煩。 在一個具有不同上下文的describe塊中,我想在一個上下文中工作,在其中放置兩個嵌套上下文,這將改變其外部上下文中的變量。

context 'and the attachment is a zipfile' do下面的代碼中, context 'and the attachment is a zipfile' do是我要工作的describe塊的上下文。我想通過為'application/x-zip-compressed'使用兩個不同的上下文來更改變量contentType 'application/x-zip-compressed''application/octet-stream'

我可以設置兩個內部上下文來使用let(:contentType)來更改contentType變量,還是不能正常工作,因為這是我理解為不能更改的常量的符號?

最初,這只是將"file1"哈希type設置為"applicatoin/x-zip-compressed" 我正在探索一種使用共享示例的方法,使類型成為可以根據上下文而更改的變量。

  context 'and 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' }
    end

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


    it 'saves each zipped file as a separate payload' do
      expect(TransactionPayload.all.size).to eq 2

是的,您可以在各種上下文中設置用let定義的變量以獲得各種行為。 但是您應該將定義的后代上下文塊括在使用該變量的位置。 但是要使用它it 必須在變量定義器上下文中聲明it子句:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM