krishna krishna - 2 months ago 9
Ruby Question

RSpec returns all objects to be 'nil'

I am new to Rspec. I am writing a test case to cover some action in a model. Here is my rspec code

test_cover_image_spec.rb

require 'spec_helper'

describe Issue do

before :each do
@issue = Issue.joins(:multimedia).uniq.first
binding.pry
end

describe '#release_cover_image' do
context 'While making an issue open' do
it 'should make issue cover in S3 accessible' do
put :update, :id => @issue.id, :issue => @issue.attributes = {:open => '1'}
end
end
end

end


@issue always returns nil. In my debugger also, Issue.all returns an empty array.

Answer

Tests usually run in isolation. That means each test needs to set up the objects before running. After the test run common test configurations delete all created data from the test database. That means you need to create your test data before you can use it.

For example like this:

require 'spec_helper'

describe Issue do
  # pass all attributes to create a valid issue
  let(:issue) { Issue.create(title: 'Foo Bar') }

  describe '#release_cover_image' do
    context 'While making an issue open' do
      it 'should make issue cover in S3 accessible' do
        put :update, id: issue.id, issue: { open: '1' }

        expect(issue.reload.open).to eq('1')
      end
    end
  end
end
Comments