shoulda

rails attr_accessible rspec check

假装没事ソ 提交于 2019-12-22 05:51:11
问题 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 ? ..

Testing devise with shoulda

為{幸葍}努か 提交于 2019-12-22 01:55:49
问题 I'm having some difficulties in testing devise with shoulda: 2) Error: test: handle :index logged as admin should redirect to Daily page. (Admin::DailyClosesControllerTest): NoMethodError: undefined method `env' for nil:NilClass devise (1.0.6) [v] lib/devise/test_helpers.rb:52:in `setup_controller_for_warden' I have this in my test_helper: include Devise::TestHelpers Thoughts ? Thanks in advance, Cristi 回答1: include Devise::TestHelpers doesn't go in the test_helper.rb file, but rather inside

Do shoulda-matchers' ActiveRecord matchers violate the “test behavior not implementation” rule?

久未见 提交于 2019-12-19 22:09:12
问题 For example, if I am using should validate_presence_of in my spec, that's only testing that I have the validate_presence_of piece of code inside my model, and that's testing implementation. More importantly, isn't that spec totally useless for testing the real problem, which is "if I don't fill out a certain field, will the model be saved successfully?" 回答1: Some of shoulda-matchers' matchers don't test implementation, they test behavior. For example, look at the source for allow_value (which

Testing associations with rspec-rails 3.0.1 and shoulda doesn't work

≡放荡痞女 提交于 2019-12-14 03:47:48
问题 Currently, with rspec-rails (2.14.2), I test my associations in model specs with the shoulda (3.5.0) gem like so: # app/models/user.rb class User < ActiveRecord::Base belongs_to :school end # spec/models/user_spec.rb describe User do it { should belong_to :school } end After some research, I hit a wall trying to make association-related assertions work (they all seem to fail). Error message: 1) User Failure/Error: it { is_expected.to belong_to :school } NoMethodError: undefined method `belong

rspec + shoulda: setting up data

笑着哭i 提交于 2019-12-13 01:58:18
问题 I have the following test. There are three it blocks. The first one doesn't use shoulda unlike the other two. If I don't use the before block with post :create, product: attrs then the first test fails as expected. But If I put the before block there then the first test fails, but the other two pass. I have a uniqueness validation on product name, but that shouldn't be the problem as I'm using sequence with factory. What should I do? How should I generally setup the data for testing when

Using shoulda to refactor rspec tests on Rails models

岁酱吖の 提交于 2019-12-12 07:46:49
问题 After learning about shoulda-matchers by answering another StackOverflow question on attribute accessibility tests (and thinking they were pretty awesome), I decided to try refactoring the model tests I did in The Rails Tutorial in an attempt to make them even more concise and thorough. I did this thanks to some inspiration from the documentation for modules Shoulda::Matchers::ActiveRecord and Shoulda::Matchers::ActiveModel, as well as this StackOverflow answer on structuring shoulda tests in

Shoulda-matcher How to validate uniqueness of enum attribute?

China☆狼群 提交于 2019-12-12 02:21:55
问题 I use rspec-rails with shoulda-matcher to test my model. Here is the code: user_ticket.rb class UserTicket < ActiveRecord::Base belongs_to :user belongs_to :ticket enum relation_type: %w( creator supporter ) validates_uniqueness_of :relation_type, scope: [:user_id, :ticket_id] end user_ticket_spec.rb RSpec.describe UserTicket, type: :model do subject { FactoryGirl.build(:user_ticket) } describe 'Relations' do it { should belong_to(:user) } it { should belong_to(:ticket) } end describe

One-liner shoulda syntax using braces

依然范特西╮ 提交于 2019-12-12 01:18:58
问题 In the book Rails Test Prescriptions (b10.0, page 176), there are examples of one-liner assertions like the following: should "be successful" { assert_response :success } This doesn’t appear to be valid ruby syntax to me, and ruby reports that the left curly brace is unexpected. In order for it to be parsed, I have to change it to should "be successful"; do assert_response :success end What's wrong with the syntax of the first example? 回答1: This is valid Ruby syntax. Well, sort of. It just

how to test a method of models with rspec and factory

拈花ヽ惹草 提交于 2019-12-12 00:33:06
问题 I'm newbie on rails and I have to write tests for existing rails apps with 'Rspec','shoulda' and 'factory girl' gems. I can test non specific tests like validates_presence_of: something with 'sholda' matchers. But I want to test methods which in models. I can visualize what I need to do, but I can't compose. This is an example what I'm talking about: . . . context 'is editable if project is not started' do setup do @brief=Factory(:brief) @started_project=Factory(:project_started, :brief =>

Shoulda with Rspec Gem returning “undefined method `reflect_on_association' for String:Class” in belong_to test

耗尽温柔 提交于 2019-12-11 05:18:56
问题 In my rails app, I have my models Request , Service , and ServiceRequest In my models.rb files I have: request.rb : class Request < ApplicationRecord validates_presence_of :userid, :supervisor, :status has_many :servicerequests, dependent: :destroy accepts_nested_attributes_for :servicerequests end service.rb : class Service < ApplicationRecord validates_presence_of :title, :responsible has_many :servicerequests, dependent: :destroy end servicerequest.rb : class Servicerequest <