Question

Most of my test is working, but for some reason the FactoryGirl.build ... should_not be_valid isn't working at the end of this statement

describe "exceed the maximum number of subscriptions" do 
  @user = FactoryGirl.create(:user)
  loop_count = GlobalVar::MAX_SUBSCRIPTIONS
  loop_count.times do 
    @topic = FactoryGirl.create(:topic)
    @subscription = FactoryGirl.create(:subscription, topic: @topic, user: @user)
  end
  @topic = FactoryGirl.create(:topic)
  FactoryGirl.build(:subscription, topic: @topic, user: @user).should_not be_valid
end

In the same spec this passes successfully:

it "has a maximum length bio" do 
    FactoryGirl.build(:user, bio: "a"*251).should_not be_valid
end

Here is the start of the error I'm getting:

(druby://192.168.1.118:53053) C:/Sites/mavens/spec/models/user_spec.rb:42:in `block (3 levels) in <top (required)>': undefined local variable or method `be_valid' for #<Class:0x7e49290> (NameError)
    from (druby://192.168.1.118:53053) C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.11.1/lib/rspec/core/example_group.rb:238:in `module_eval'

I'm using Spork and Guard to test. I have FactoryGirl reloaded in spec_helper for each run. Have restarted spork several times and it does not work with the restart. Let me know if any of my additional code would be helpful, and as always many thanks for the help!!

Was it helpful?

Solution

You need to put tests inside it blocks:

describe "exceed the maximum number of subscriptions" do 
  it do
    user = FactoryGirl.build(:user)
    GlobalVar::MAX_SUBSCRIPTIONS.times do
      topic = FactoryGirl.build(:topic)
      FactoryGirl.build(:subscription, topic: topic, user: user)
    end
    topic = FactoryGirl.build(:topic)
    FactoryGirl.build(:subscription, topic: topic, user: user).should_not be_valid
  end
end

This is due to how RSpec handles the context of the DSL.

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