I
I
IvanN7772015-02-13 13:07:56
Ruby on Rails
IvanN777, 2015-02-13 13:07:56

Problem with configuration gem 'factory_girl_rails' and 'rspec-rails' plz see what's wrong?

Connected two gems
gem 'factory_girl_rails'
gem 'rspec-rails'
According to
everydayrails.com/2012/03/19/testing-series-rspec-...
To spec/spec_helper.rb
Added

require 'factory_girl_rails'
#....
 config.include FactoryGirl::Syntax::Methods

Added a new directory "spec/factories"
In it I decided to check the Pool test model with a single name attribute
In the file "spec/factories/pools.rb" I added
FactoryGirl.define do
  factory :pool do |f|
    f.name "Ivan"
  end
end

In the file "spec/models/pool_spec.rb"
require 'rails_helper'

RSpec.describe Pool, type: :model do
  pending "add some examples to (or delete) #{__FILE__}"
  it "has a valid factory" do
    FactoryGirl.create(:pool).should be_valid
  end
end

When I tried to figure it out, I deleted the "spec/factories" directory.
The error did not change.
I suspect that when "spec/factories" existed, for some reason the loading did not occur.
Someone does not test these things?
When running the test, it gives an error:
1) Pool has a valid factory
Failure/Error: FactoryGirl.create(:pool).should be_valid
NoMethodError:
undefined method `name=' for #

Answer the question

In order to leave comments, you need to log in

3 answer(s)
J
Jeiwan, 2015-02-14
@Jeiwan

Old guide. In general, for gems, in most cases, manuals are not needed: there is a repository on github, where everything is written. For example, here is the factory_girl repository - https://github.com/thoughtbot/factory_girl , here is the actual cheat sheet - https://github.com/brennovich/cheat-ruby-sheets/bl...

factory :pool do |f|
  f.name "Ivan"
end

Obsolete syntax, now you do not need to pass a variable to the block, and accordingly the fields are assigned directly. Most likely, the error occurs precisely because of this.
This is no longer needed either.
After that:
write FactoryGirl here:
no need. You can just `create(:pool)`

V
Viktor Vsk, 2015-02-13
@viktorvsk

Do you have a Pool model with a method name=?
In app/models/pool.rb

I
IvanN777, 2015-02-13
@IvanN777

class CreatePools < ActiveRecord::Migration
def change
create_table :pools do |t|
t.string :name
t.timestamps null: false
end
end
end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question