2016-04-26 37 views
0

私はRuby/Railsを勉強しています。私の作品のコードベースを理解し、テストに追加することから始めます。私は、共有の例を設定する方法を理解することにいくつかの問題を抱えています。異なるコンテキストを持つ記述ブロックで、私は1つのコンテキスト内で作業したいので、その内部に変数を変更する2つのネストされたコンテキストを入れます。 context 'and the attachment is a zipfile' do以下のコードでRSpecテストの共有サンプル内で共有サンプルを作成するために、コンテキスト内にコンテキストをネストする方法(すでに別のコンテキストにある)

は私が仕事をしたい記述ブロックのコンテキストである。私は'application/x-zip-compressed''application/octet-stream'ための2つの異なるコンテキストを有することによって可変contentTypeを変更します。

let(:contentType)でcontentType変数を変更するために2つの内部内部コンテキストを設定することはできますか、それは変更できない定数として理解できるシンボルなので動作しませんか?

元々、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 
+0

はい、あなたはセットアップ変数は、様々な動作を得るために、様々なコンテキスト内でletで定義されることができます。 –

答えて

0

はい、letで定義された変数をさまざまなコンテキスト内で設定して、さまざまな動作を得ることができます。しかし、その定義は、変数が使用されている場所に子孫コンテキストブロックを囲む必要があります。しかし、それを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 
関連する問題