shoulda

How to test scopes?

情到浓时终转凉″ 提交于 2019-12-03 09:40:41
问题 tried to find but with no success. Just wondering how could I test scopes in Rails 3. Could be using rspec, shoulda or just a test unit. Thanks. Actually, I trying this way, but it's not complete test since it's still need to put the order() method. The Scope: scope :recents_available, where(:available => true, :locked => false).order("created_at DESC") describe Job, ":recents_available" do it "should have the scope" do Job.should respond_to(:recents_available) end it "should include recents

How can I test :inclusion validation in Rails using RSpec

旧时模样 提交于 2019-12-03 05:32:06
问题 I have the following validation in my ActiveRecord. validates :active, :inclusion => {:in => ['Y', 'N']} I am using the following to test my model validations. should_not allow_value('A').for(:active) should allow_value('Y').for(:active) should allow_value('N').for(:active) Is there a cleaner and more through way of testing this? I am currently using RSpec2 and shoulda matchers. EDIT After some looking around I only found, this probably an 'ok' way of testing this, shoulda does not provide

How do you specify POST params in a Rails test?

百般思念 提交于 2019-12-03 04:16:56
问题 Working with Test::Unit and Shoulda. Trying to test Users.create . My understanding is that Rails forms send params for an object like this: user[email] Which turns into hash in your action, right? params[:user][:email] OK, so in my test I've tried... setup { post :create, :post => { 'user[email]' => 'invalid@abc' } } and setup { post :create, :post => { :user => { :email => 'abc@abcd' } } } In both cases, over in my action, params[:user] is nil. 回答1: post :create, :user => { :email => 'foo

shoulda-matchers RSpec expect syntax

五迷三道 提交于 2019-12-03 02:20:39
问题 What is the correct format for using shoulda-matchers and RSpec's new expect syntax? 回答1: While one could certainly use the shoulda-matchers with the new expect syntax as follows: it 'should validate presence of :email' do expect(subject).to validate_presence_of :email end or the more concise but less readable: it { expect(subject).to validate_presence_of :email } the one-liner should format these matchers are typically used with is explicitly supported in 2.14 even when config.syntax ==

Shoulda/RSpec matchers - conditional validation

冷暖自知 提交于 2019-12-03 01:28:33
问题 In my code I had the following validation with Shoulda matchers, which works fine: it { should validate_presence_of(:name) } In my model, I've added the condition to my validation: validates_presence_of :name, :if => eligible? Is it possible to reflect it in the validations? I've tried looking at documentation for shoulda matchers, but haven't been able to locate the solution. Many thanks! 回答1: It doesn't appear that shoulda_matchers does this, but it's easy enough to write it yourself::

shoulda-matchers RSpec expect syntax

给你一囗甜甜゛ 提交于 2019-12-02 15:52:05
What is the correct format for using shoulda-matchers and RSpec's new expect syntax ? Peter Alfvin While one could certainly use the shoulda-matchers with the new expect syntax as follows: it 'should validate presence of :email' do expect(subject).to validate_presence_of :email end or the more concise but less readable: it { expect(subject).to validate_presence_of :email } the one-liner should format these matchers are typically used with is explicitly supported in 2.14 even when config.syntax == :expect . When should is being used with an implicit subject as in: describe User it { should

Shoulda/RSpec matchers - conditional validation

牧云@^-^@ 提交于 2019-12-02 14:49:10
In my code I had the following validation with Shoulda matchers, which works fine: it { should validate_presence_of(:name) } In my model, I've added the condition to my validation: validates_presence_of :name, :if => eligible? Is it possible to reflect it in the validations? I've tried looking at documentation for shoulda matchers, but haven't been able to locate the solution. Many thanks! zetetic It doesn't appear that shoulda_matchers does this, but it's easy enough to write it yourself:: context "if eligible" do before { allow(subject).to receive(:eligible?).and_return(true) } it { should

How do I get shoulda to recognise my polymorphic association

南楼画角 提交于 2019-12-02 11:29:51
问题 An almost indentical question to this has been asked before (How to use shoulda matchers to test a polymorphic assoication?) but there was no definitive answer that helps me, so I am trying again. I am using shoulda to test my associations and the following test fails require 'spec_helper' describe LabourEpidural do before {@treatment = FactoryGirl.build :treatment} subject {@treatment} it{should have_many :complications} end This fails with the following message Failure/Error: it{should have

Rspec / shoulda: testing, that a custom validator is called

烈酒焚心 提交于 2019-12-02 06:14:27
问题 I have a custom validator (located in app/validators/uri_validator.rb) which is used in: validates :link, uri: true How do I specify this in my specs? Ideally I would like to have a one-line call, such as: it { should validate_uri_of(:link) } How do I do this? 回答1: Another option is to use the allow_value matcher, although not ideal it can work in some circumstances. it { should allow_value(value_which_is_valid).for(:link) } it { should_not allow_value(value_which_is_invalid).for(:link) } 回答2

How do I get shoulda to recognise my polymorphic association

二次信任 提交于 2019-12-02 04:42:52
An almost indentical question to this has been asked before ( How to use shoulda matchers to test a polymorphic assoication? ) but there was no definitive answer that helps me, so I am trying again. I am using shoulda to test my associations and the following test fails require 'spec_helper' describe LabourEpidural do before {@treatment = FactoryGirl.build :treatment} subject {@treatment} it{should have_many :complications} end This fails with the following message Failure/Error: it{should have_many :complications} Expected Treatment to have a has_many association called complications