Question

I created two very similar tables. One of them is called performer and the other one is performance. Performer only contains a name:string. While Performance contains file_name:string, date:date and location:string. I have very similar rspec files for both of them and the Performer test passes with no issues however the performance fails the test of all 3 attributes. When i enter sandbox and test the attributes with

performance = Performance.new performance.respond_to?(:file_name) it returns true.

Yet in rspec it fails. Here are the failures.

1) Performance
     Failure/Error: it { should respond_to(:date) }
       expected Performance(id: integer, file_name: string, date: date, location
: string, created_at: datetime, updated_at: datetime) to respond to :date
     # ./spec/models/performance_spec.rb:10:in `block (2 levels) in <top (requir
ed)>'

  2) Performance
     Failure/Error: it { should respond_to(:file_name) }
       expected Performance(id: integer, file_name: string, date: date, location
: string, created_at: datetime, updated_at: datetime) to respond to :file_name
     # ./spec/models/performance_spec.rb:9:in `block (2 levels) in <top (require
d)>'

  3) Performance
     Failure/Error: it { should respond_to(:location) }
       expected Performance(id: integer, file_name: string, date: date, location
: string, created_at: datetime, updated_at: datetime) to respond to :location
     # ./spec/models/performance_spec.rb:11:in `block (2 levels) in <top (requir
ed)>'

here is my rspec file:

require 'spec_helper'

describe Performance do
   before { @performance = Performance.new(file_name: "Example Performece", 
                                               date: DateTime.parse("2011-06-02T23:59:59+05:30").to_date,
                                           location: "lame house", ) }
    subject { Performance }

    it { should respond_to(:file_name) }
    it { should respond_to(:date) }
    it { should respond_to(:location) }
end

My performer file is extremely similar, Is the date attribute somehow messing this thing up? I tried creating an empty performance with Performance.new but it didnt fix the problem so i dont know whats going on.

If you want to see my files comment below and ill post them here. Im by the computer so i should be quick to respond

Was it helpful?

Solution

When you use one-liner syntax like:

it { should have_something }

It is exactly like you wrote

it 'should have something' do
  subject.should have_something
end

In your code you have declared your subject to be the class Performance:

subject { Performance }

This means that your tests are actually:

it 'should respond to file_name' do
  Performance.should respond_to(:file_name)
end

Which, of course, it doesn't.

When you set the subject to @performance

subject { @performance }

the test becomes:

it 'should respond to file_name' do
  @performance.should respond_to(:file_name)
end

which should work as expected.

OTHER TIPS

subject should be

subject { Performance.new(file_name: "Example Performece", date: DateTime.parse("2011-06-02T23:59:59+05:30").to_date, location: "lame house", ) }

that's all

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top