I try to create test for users-factories.But when I running test, 'rspec' show me an error
Failure/Error: expect(:user).to be_valid
NoMethodError:
undefined method `valid?' for :user:Symbol
this is my user_spec.rb
require 'rails_helper'
RSpec.describe do
it "has a valid factory" do
user = FactoryGirl.create(:user)
expect(:user).to be_valid
end
end
and this is users-factories(with 'faker' gem)
FactoryGirl.define do
factory :user do
email Faker::Internet.email
password Faker::Internet.password(8)
end
end
how fix and why this method don't work?
sorry for my bad English
Here is a fix :
RSpec.describe do
it "has a valid factory" do
user = FactoryGirl.create(:user)
expect(user).to be_valid
end
end
You passed the symbol :user as an argument to the expect(:user), which you shouldn't. You should pass the local variable user, you have created, and passed to it expect(user).
Related
Tested code:
context 'validations' do
let(:super_campaign){FactoryGirl.create(:super_campaign)}
context 'package' do
it "Package should be present" do
expect(super_campaign.package).to be_valid
end
end
end
I am getting error like undefined method "valid?" for "possimus":String
expect(super_campaign).to be_valid # AR object, not it's attribute
You can only test that the whole model is valid. You can test if you have an error for the package attribute, though.
context 'validations' do
let(:super_campaign){FactoryGirl.create(:super_campaign)}
context 'package' do
it "Package should be present" do
expect(super_campaign).to be_valid
expect(super_campaign.errors[:package]).to be_nil
end
end
end
I have this spec that I want to translate to MiniTest.
describe User do
subject { build(:user, provider: 'foo') }
# don't validate presence of password when provider is present
it do
should_not validate_presence_of(:password)
end
end
I tried this. I am getting an error of undefined method 'should_not' for UserTest
class UserTest < ActiveSupport::TestCase
def setup
#user = build_stubbed(:user)
end
test "responds to name" do
assert_respond_to #user, :name
end
should validate_presence_of(:password)
test "do not validate presence of password when provider is present" do
build_stubbed(:user, provider: 'foo')
should_not validate_presence_of(:password)
end
end
I want to change the context for one test, where the subject gets a provider attribute, which should disable the presence validator on the password field.
Here's the full error:
UserTest#test_presence_of_password:
NoMethodError: undefined method `should_not' for #<UserTest:0x007feaa82c1c68>
test/models/user_test.rb:45:in `block in <class:UserTest>'
I found that the better way to do this is to revert to good old MiniTest:
test "uniqueness of email with a different provider" do
email_user = create(:user, email: "foo#bar.com")
facebook_user = build_stubbed(:facebook_user, email: "foo#bar.com")
assert facebook_user.valid?, "should be valid with same email if provider is different"
end
Take a look at the minitest-rails-shoulda gem. If you use it I assume the test would look like this:
describe User do
subject { build_stubbed(:user) }
it { must validate_presence_of(:password) }
describe "when a provider is present" do
subject { build_stubbed(:user, provider: 'foo') }
it { wont validate_presence_of(:password) }
end
end
I am at trying to set up testing in my app, and I have run into a problem with RSpec, FactoryGirl, and Mongoid. I have the following factory:
FactoryGirl.define do
factory :user do |u|
u.name { Faker::Name.name }
u.email { Faker::Internet.email }
u.crypted_password { Faker::Lorem.characters(10) }
u.password_salt { Faker::Lorem.characters(10) }
u.role :user
end
end
I try to use this factory in my tests:
require 'spec_helper'
describe User do
it "has a valid factory" do
create(:user).should be_valid
end
end
But I get this error:
1) User has a valid factory
Failure/Error: FactoryGirl.create(:user).should be_valid
NoMethodError:
undefined method `user' for #<User:0x007ff24a119b28>
# ./spec/models/user_spec.rb:5:in `block (2 levels) in <top (required)>'
I don't know what is causing this error. Also, is there a way to see a full stacktrace using rspec?
This line has problem
u.role :user
I guess you want to define a default role as "user"? Then don't use symbol or method, use string instead
u.role 'user'
I am trying to verify that the validators are working correctly on my model, and for that I am using Rspec and Capybara. Here is my code.
describe "#when registering" do
before { visit new_record_path }
describe "#with invalid information" do
describe "#should not modify database" do
subject { -> { click_button submit } }
it { should_not change(Pet, :count) }
it { should_not change(Owner, :count) }
end
end
end
end
When I run the specs, i get an error: "undefined method 'model_name' for NilClass:Class"
What could be causing rspec to think my model is nil?
Thanks!
You should not test your validations with a feature/acceptance test, it should be with a model test. Then for each form you could test an error is raised if something is invalid instead of testing every error through acceptance tests. For each model it should be something like so:
describe Pet do
describe "validations" do
# These can echo any model validation
it "is invalid if attribute is not present" do
Pet.new(:attribute => "Invalid Item").should_not be_valid
end
end
end
or with Factory Girl:
describe Pet do
describe "validations" do
it "is invalid if attribute is not present" do
build(:pet, :attribute => "Invalid Item").should_not be_valid
end
end
end
Then in an acceptance test you can have something like:
it "displays an error if validation fails" do
visit new_pet_path
#Something to make the form submission fail, not everything
fill_in("Attribute", :with => "")
click_button("Create Pet")
page.should have_content("can't be blank")
current_path.should == pets_path
end
This will help to keep your acceptance tests light and test the validations in the model where it belongs. Hope this helps!
I have ran into a quick problem. I am getting a "undefined method `Factory'" for the user_spec page. I believe I have the incorrect syntax for " let(:user) { Factory(:user) }". However I can't come up with one that works.
Failure/Error: let(:user) { Factory(:user) }
NoMethodError:
undefined method `Factory' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fc7ac31e6a8>
If someone can take a look at my code and help me out that would be appreciated as I am almost done testing for the password reset.
user_spec.rb:
require 'spec_helper'
describe User do
describe "#send_password_reset" do
let(:user) { Factory(:user) }
it "generates a unique password_reset_token each time" do
user.send_password_reset
last_token = user.password_reset_token
user.send_password_reset
user.password_reset_token.should_not eq(last_token)
end
it "saves the time the password reset was sent" do
user.send_password_reset
user.reload.password_reset_sent_at.should be_present
end
it "delivers email to user" do
user.send_password_reset
last_email.to.should include (user.email)
end
end
end
the Factory Girl method is the following:
let(:user) { FactoryGirl.create(:user) }
Replace Factory with FactoryGirl, they changed that to remove ambiguity and to let the useful Factory namespace available.