shoulda

Shoulda matchers have_many with custom relation name

試著忘記壹切 提交于 2019-12-06 03:40:17
问题 How do I test this ActiveRecord relation using shoulda matchers? Models class User < ActiveRecord::Base has_many :articles end class Article < ActiveRecord::Base belongs_to :author, class_name: 'User' end Test describe User do it { should have_many(:articles) } end I'm getting the following error: 1) User should have many articles Failure/Error: it { should have_many(:articles) } Expected User to have a has_many association called articles (Article does not have a user_id foreign key.) # .

Shoulda validate_format_of . not_with has problem in framework (or in my understanding)

不羁的心 提交于 2019-12-06 01:21:12
问题 I put the following code into an RSpec test: it { should validate_format_of(:email).not_with('test@test')} and setup the actual class with: validates :email, :presence => true, :format => /\b[A-Z0-9._%-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b/i And when I run the tests I get: Failures: 1) User Failure/Error: it { should validate_format_of(:email).not_with('test@test')} Expected errors to include "can't be blank" when email is set to "test@test", got errors: ["name can't be blank (nil)", "email is

Shoulda rspec matchers :on => :create

て烟熏妆下的殇ゞ 提交于 2019-12-05 22:02:11
问题 I am using some of the Shoulda rspec matchers to the test my model, one of them being: describe Issue do it { should_not allow_value("test").for(:priority) } end My problem with this is that my validation in my model looks like this: validates_format_of :priority, :with => /^(Low|Normal|High|Urgent)$/, :on => :update So when running this test I get: 1) 'Issue should not allow priority to be set to "test"' FAILED Expected errors when priority is set to "test", got errors: category is invalid

How can I generate a report that shows me my slowest running tests in Rails 3.2, Ruby 1.9?

我怕爱的太早我们不能终老 提交于 2019-12-05 16:09:34
I know that RSpec has the --profile option, but I'm only using MiniTest/shoulda for my current project. You can use minitest-reporters for this purpose. This gem provide multiple reporters to see output of your tests. Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new Spec reporter shows the time which each test take to run. It shows the time on console, not as a report. You can just use: rake TESTOPTS="-v" right out of the box. eg: rake TESTOPTS="-v" test:controllers I have tested this with Ruby 1.9.3 on Rails 3.2. 来源: https://stackoverflow.com/questions/23620763/how-can-i

How to use shoulda matchers to test a polymorphic assoication?

吃可爱长大的小学妹 提交于 2019-12-05 11:10:29
问题 I'm using shoulda-matchers with rails and I'm creating a model called "comments" and another model called "post". Comments is polymorphic. When I test with shoulda matchers in post like this it {should have_many(:comments)} it get this message Expected Post to have a has_many association called comments (Comment does not have a post_id foreign key.) In my comment model I have belongs_to :commentable, :polymorphic => true How can I test my polymorphic association so that a post can have many

rails attr_accessible rspec check

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 07:30:51
When I want to test if attribute is / is not accessible with RSpec I'm doing it like this class Foo attr_accesible :something_else end describe Foo do it('author should not be accessible') {lambda{described_class.new(:author=>true)}.should raise_error ActiveModel::MassAssignmentSecurity::Error} it('something_else should be accessible'){lambda{described_class.new(:something_else=>true)}.should_not raise_error ActiveModel::MassAssignmentSecurity::Error} end is there better way doing that ? ...thx This is the way attribute accessibility tests are done in the Rails Tutorial , which I think are

Shoulda/RSpec: Make sure that validation message “xxx” is on :base

点点圈 提交于 2019-12-04 20:40:16
I'm still pretty confused about what is magic behind stuff like it { should have(1).error_on(:base) } and what's a specific Shoulda matcher. I'd like to make sure that :base contains the error message "xxx", so how should I do this? it "should contain error message 'xxx'" do contact.valid? contact.errors[:base].should include('xxx') end Is this "the way to go", or is there a better one? Thanks. Right, it's looking good. Inline rspec tests are using subject. You could rewrite your test like this: describe 'my method' do before { contact.valid? } context 'contact is not valid' do subject {

when does factory girl create objects in db?

浪子不回头ぞ 提交于 2019-12-04 08:53:12
问题 I am trying to simulate a session using FactoryGirl / shoulda (it worked with fixtures but i am having problems with using factories). I have following factories (user login and email both have unique validations): Factory.define :user do |u| u.login 'quentin' u.email 'quentin@example.com' end Factory.define :session_user, :class => Session do |ses| ses.association :user, :factory => :user ses.session_id 'session_user' end and here's the test class MessagesControllerTest < ActionController:

Shoulda validate_format_of . not_with has problem in framework (or in my understanding)

女生的网名这么多〃 提交于 2019-12-04 05:47:06
I put the following code into an RSpec test: it { should validate_format_of(:email).not_with('test@test')} and setup the actual class with: validates :email, :presence => true, :format => /\b[A-Z0-9._%-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}\b/i And when I run the tests I get: Failures: 1) User Failure/Error: it { should validate_format_of(:email).not_with('test@test')} Expected errors to include "can't be blank" when email is set to "test@test", got errors: ["name can't be blank (nil)", "email is invalid (\"test@test\")"] # ./spec/models/user_spec.rb:8:in `block (2 levels) in ' When I do a passing

Shoulda rspec matchers :on => :create

╄→гoц情女王★ 提交于 2019-12-04 02:53:27
I am using some of the Shoulda rspec matchers to the test my model, one of them being: describe Issue do it { should_not allow_value("test").for(:priority) } end My problem with this is that my validation in my model looks like this: validates_format_of :priority, :with => /^(Low|Normal|High|Urgent)$/, :on => :update So when running this test I get: 1) 'Issue should not allow priority to be set to "test"' FAILED Expected errors when priority is set to "test", got errors: category is invalid (nil)title can't be blank (nil)profile_id can't be blank (nil) The validation isn't being triggered