Rspec failure when it should be passing
When I run bundle exec rspec spec/ I have one of my tests fail that should be passing. Here's the error:
Failure/Error: @user = Factory(:user)
NoMethodError:
undefined method `Factory' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_2:0x1037c0a70>
# ./spec/controllers/users_controller_spec.rb:21
Here's the test code:
describe "GET 'show'开发者_Python百科" do
before(:each) do
@user = Factory(:user)
end
it "should be successful" do
get :show, :id => @user
response.should be_success
end
it "should find the right user" do
get :show, :id => @user
assigns(:user).should == @user
end
end
I installed the Factory Girl gem, so I'm unsure what the issue is.
You say you installed factory_girl, but is it in both your Gemfile and Gemfile.lock files? (You may need to bundle install to get it into Gemfile.lock). Since you're using bundle exec, bundler will check your Gemfile.lock for the gems it should load.
[Edit]
On second thought, this looks like a duplicate of this SO question.
This may be not the case, but have you tried restarting the web server ?
The answers in this did not help me. What I found to solve my problem was to add a line to spec_helper.rb
require 'factory_girl'
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
Then in my spec's I would call it like so
person = build(:person)
Hope that helps someone. Was looking for about 3 hours.
I think, that this is a correct solution:
@user = Factory(:user)
Replace to
@user = Factory.create(:user)
加载中,请稍侯......
精彩评论