i have two error when i run rake test:
1)Error
test_should_require_a_following_id(RelationTest)
NoMethodError:underfined method 'assert_not' for #<RelationTest:0x3dd0058>
2)Error
test_should_require_a_follower_id(RelationTest)
NoMethodError:underfined method 'assert_not' for #<RelationTest:0x3c1d700>
this is my relation_test.rb
require 'test_helper'
class RelationTest < ActiveSupport::TestCase
def setup
#relation = Relation.new(follower_id: 9, following_id: 10)
end
test "should be valid" do
assert #relation.valid?
end
test "should require a follower_id" do
#relation.follower_id = nil
assert_not #relation.valid?
end
test "should require a following_id" do
#relation.following_id = nil
assert_not #relation.valid?
end
end
my relation model
class Relation < ActiveRecord::Base
attr_accessible :following_id, :follower_id
belongs_to :follower, :class_name => "User"
belongs_to :following, :class_name => "User"
validates :follower_id, presence: true
validates :following_id, presence: true
end
What can i do to fix that, please help me!
I think the problem is my rail vesion(3.2.19), but assert #relation.valid?still work??
assert_not has been added in rails 4.0.2. You need to do this instead:
assert !#relation.valid?
Related
I am starting to get into testing with minitest in Rails. Currently my first test to see if my model is valid is returning false. The error message itself is also very generic Expected false to be truthy. I have tested everything else and all of those tests work fine. Does anybody know what could be causing this?
article_test.rb
require 'test_helper'
class ArticleTest < ActiveSupport::TestCase
def setup
#article = Article.new(title:"Avengers Endgame", body:"I am inevitable")
end
test "article should be valid" do
assert #article.valid?
end
test "title should be present" do
#article.title = " "
assert_not #article.valid?
end
test "body should not be too short" do
#article.body = "aa"
assert_not #article.valid?
end
end
article.rb
class Article < ApplicationRecord
include Visible
belongs_to :user
has_many :comments, dependent: :destroy
has_rich_text :body
validates :title, presence: true
validates :body, presence: true, length: { minimum: 10 }
end
The main problem here is that you're using a poor method for testing your validations.
assert #article.valid? and assert_not #article.valid? does not actually tell you anything at all about the object under test. When the test fails you're none the wiser about why it actually failed and if the failure is actually even connected to what you're proportedly testing. At best it serves as a sort of litmus test that your test setup is correct.
Instead of this "carpet bombing" approach test each validation on its own:
class ArticleTest < ActiveSupport::TestCas
test "title should be present" do
article = Article.new(title: '')
article.valid?
assert_includes build_article.errors[:title], "can’t be blank"
end
test "body should not be too short" do
article = Article.new(body: 'aa')
article.valid?
assert_includes article.errors[:body], "is too short"
end
end
Testing all the validations at once (creating a record with valid input) will be covered by your integration and system tests anyways.
You have belongs_to :user, which expects the #article to have a user_id compulsorily to be present, before it can be saved.
If user_id is optional during creation, change this:
belongs_to :user
to
belongs_to :user, optional: true
I'm super new to testing my app using RSpec and I'm trying to test the validation of a comment without a user and keep getting syntax errors.
Here is the comment model code.
class Comment < ApplicationRecord
belongs_to :user
belongs_to :product
scope :rating_desc, -> { order(rating: :desc) }
validates :body, presence: true
validates :user, presence: true
validates :product, presence: true
validates :rating, numericality: { only_integer: true }
after_create_commit { CommentUpdateJob.perform_later(self, user) }
end
and here is the comment spec:
require 'rails_helper'
describe Comment do
before do
#product = Product.create!(name: "race bike", description: "fast race bike")
#user = User.create!(email: "jerryhoglen#me.com", password: "Maggie1!")
#product.comments.create!(rating: 1, user: #user, body: "Awful bike!")
end
it "is invalid without a user"
expect(build(:comment, user:nil)).to_not be_valid
end
end
What you're doing here is good - building objects and using the be_valid matcher. But if you use shoulda-matchers there's a one-liner to test a model validation:
describe Comment do
it { is_expected.to validate_presence_of :user }
end
You can do this for other validations such as uniqueness, numericality, etc, though you'd have to look up the syntax.
you missed a do, do it like:
it "is invalid without a user" do
expect(build(:comment, user: nil)).to_not be_valid
end
But that's not a very clear test when it fails, I suggest you check the actual expected validation error.
That's what it may look like:
expect(ValidatingWidget.new.errors_on(:name)).to include("can't be blank")
expect(ValidatingWidget.new(:name => "liquid nitrogen")).to have(0).errors_on(:name)
See rspec-rails errors_on # relishapp
In our Rails app, we have the following models:
class User < ActiveRecord::Base
has_many :administrations, dependent: :destroy
has_many :calendars, through: :administrations
end
class Administration < ActiveRecord::Base
belongs_to :user
belongs_to :calendar
end
class Calendar < ActiveRecord::Base
has_many :administrations, dependent: :destroy
has_many :users, through: :administrations
end
We tried to validate the Administration model with the following administration_test.rb test file:
require 'test_helper'
class AdministrationTest < ActiveSupport::TestCase
def setup
#user = users(:noemie)
#administration = Administration.new(user_id: #user.id, calendar_id: #calendar_id)
end
test "should be valid" do
assert #administration.valid?
end
test "user id should be present" do
#administration.user_id = nil
assert_not #administration.valid?
end
test "calendar id should be present" do
#administration.calendar_id = nil
assert_not #administration.valid?
end
end
When we run the test, we get the following results:
FAIL["test_calendar_id_should_be_present", AdministrationTest, 2015-06-30 07:24:58 -0700]
test_calendar_id_should_be_present#AdministrationTest (1435674298.26s)
Expected true to be nil or false
test/models/administration_test.rb:21:in `block in <class:AdministrationTest>'
FAIL["test_user_id_should_be_present", AdministrationTest, 2015-06-30 07:24:58 -0700]
test_user_id_should_be_present#AdministrationTest (1435674298.27s)
Expected true to be nil or false
test/models/administration_test.rb:16:in `block in <class:AdministrationTest>'
We are kind of lost: is this the right way to right the test?
If no, how should we write it?
If yes, how can we make it pass?
The problem is not your test but rather that you are expecting the wrong outcome.
belongs_toin ActiveRecord does not add a validation, the macro simply creates a relation.
To validate a relation you would use validates_associated which calls #valid? on each of the associated records and validates_presence_of to ensure that the associated record is present.
class Administration < ActiveRecord::Base
belongs_to :user
belongs_to :calendar
validates_associated :user
validates :user, presence: true
end
When testing validations it is better to write on the assertions on the errors hash, as assert_not #administration.valid? will give a false positive if the validation fails for any other reason.
Bad:
test "user id should be present" do
#administration.user_id = nil
assert_not #administration.valid?
end
Good:
test "user id should be present" do
#administration.user_id = nil
#administration.valid?
assert #administration.errors.key?(:user)
end
Ok, we found the solution.
We updated our Administration model as follows:
class Administration < ActiveRecord::Base
belongs_to :user
belongs_to :calendar
validates :user_id, presence: true
validates :calendar_id, presence: true
end
And also edited our administration_test.rb file:
require 'test_helper'
class AdministrationTest < ActiveSupport::TestCase
def setup
#user = users(:noemie)
#calendar = calendars(:one)
# This code is not idiomatically correct.
#administration = Administration.new(user_id: #user.id, calendar_id: #calendar.id)
end
test "should be valid" do
assert #administration.valid?
end
test "user id should be present" do
#administration.user_id = nil
assert_not #administration.valid?
end
test "calendar id should be present" do
#administration.calendar_id = nil
assert_not #administration.valid?
end
end
The tests are now passing just fine.
I have a class User as follows
class User < ActiveRecord::Base
has_one :session
has_and_belongs_to_many :posts
has_and_belongs_to_many :replies
attr_accessor :clear_text_password;
validates_presence_of :username
validates_uniqueness_of :username
validates :clear_text_password, :presence => true, :length => {:minimum => 6}
validates_confirmation_of :clear_text_password
validates_presence_of :e_mail
validates_uniqueness_of :e_mail
def User.authenticate(name, password)
if user = find_by_username(name)
if user.password == encrypt_password(password)
user
end
end
end
end
Now, my rspec test file is as below
require 'spec_helper'
require 'rspec'
require 'rspec-rails'
require 'user'
describe User do
describe "my first test" do
it "should redirect if user is authenticated" do
user = User.authenticate('abcd','abcdef')
c=false
if user
c=true
end
c.should == true
end
end
end
The database table users has the username and password "abcd" and "abcdef" . but the test always fails. the method call always returns false. Please help!!
I have the following model:
module CgCart
class ShippingInfo < ActiveRecord::Base
set_table_name 'cg_cart.shipping_infos'
has_many :order, :class_name => "CgCart::Order"
accept_nested_attributes_for :orders, :allow_destroy => true
validates_presence_of :name, :street, :city, :country, :zip, :state
def to_s
retval = <<-formatted_addr
#{self.name}
#{self.street}
#{self.city} #{self.state} #{self.zip}
#{self.country}
formatted_addr
retval.upcase
end
end
end # module
I am writing a spec test. It goes like this:
require File.dirname(__FILE__) + '/../../spec_helper'
describe CgCart::ShippingInfo do
context "when first created" do
it "should be empty" do
#shipping_info = CgCart::ShippingInfo.new
#shipping_info.should be_empty
end
end
end
When I run the spec test I get the following error:
1)
NoMethodError in 'CgCart::ShippingInfo when first created should be empty'
undefined method `be_empty' for #<Spec::Matchers::PositiveOperatorMatcher:0x105f165c0>
./spec/models/cg_cart/shipping_info_spec.rb:6:
Finished in 0.015 seconds
1 example, 1 failure
This seems like it should very straight forward. Any ideas on why this test fails?
I was trying to use empty where I should have been using nil.
#shipping_info.should be_nil