我有一个使用factory_girl_rails gem 3.2的FActory
FactoryGirl.define do
sequence(:random_string) {|n| "#{LoremIpsum.generate}#{n}" }
factory :agenda do
subject { generate(:random_string) }
due_date "2012-05-14"
completed false
user
end
end
但是我在Rspec测试中得到了一个NoNameError。我在
今天,我遇到了一个非常奇怪的问题我的规格。当我在没有指定任何特定dir/文件的情况下运行spec命令时,我的测试运行得很好,都是绿色的。但是,如果我指定一个dir或文件,rspec会抛出很少(随机的)错误,这取决于我是否在spring中使用rspec。更重要的是,昨天,就在我的工作结束之前,一切都很顺利。从那以后,我没有碰过任何东西,现在由于某种原因,它已经坏了。情况如下:
rspec spec/requests/api/v1/users_spec.rb
结果:
uninitialized constant FactoryGirl
spring stop; spring rspec s
我在这本书“用RSpec进行日常Rails测试”( Everyday Rails Testing with RSpec)中继续学习,它使用了assigns
it "assigns the requested Category to @category" do
category = FactoryGirl.create(:category)
binding.pry
get :edit, id: category
expect(assigns(:category)).to eq category
end
然而,我的测试失败了,所以我使用pry来挖掘(如上面所示)
[
我遇到了一个问题,FactoryGirl似乎正在用has_many关系创建额外的记录。
鉴于这些模式:
class NextAction < ActiveRecord::Base
has_many :next_actions_orders
has_many :orders, through: :next_actions_orders
end
class NextActionsOrder < ActiveRecord::Base
belongs_to :order
belongs_to :next_action
end
class Order < Activ
所以我有一个模型代码:
def self.cleanup
Transaction.where("created_at < ?", 30.days.ago).destroy_all
end
这个rspec单元测试:
describe 'self.cleanup' do
before(:each) do
@transaction = Transaction.create(seller:item.user, buyer:user, item:item, created_at:6.weeks.ago)
end
it
每当我想要创建一个用户时,我都想删除FactoryGirl.build(:user),所以我添加了以下行:
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
给spec_helper.rb。但是这会产生以下错误:
`method_missing': `build` is not available on an example group (e.g. a `describe` or `context` block). It is only available from within
我测试.,这个索引动作‘填充一个数组的所有问题。
require 'rails_helper'
RSpec.describe QuestionsController, type: :controller do
describe 'GET #index' do
before do
@questions = FactoryGirl.create_list(:question, 2)
get :index
end
it 'populates an array of all questions'
我已经实现了来修剪我的数据库。
# model.rb
after_create do
self.class.prune(ENV['VARIABLE_NAME'])
end
def self.prune(max)
order('created_at DESC').last.destroy! until count <= max
end
这在手工测试中效果很好。
在RSpec中,测试如下:
# spec/models/model_spec.rb
before(:each) do
@model = Model.new
end
describe
我无法解决以下rspec验证错误:
Failures:
1) User treating associations should have the right treatings in the right order
Failure/Error: FactoryGirl.create(:treating, requestor: @user, created_at: 1.day.ago)
ActiveRecord::RecordInvalid:
Validation failed: Requestee can't be blank
# .
我是rspec的新手,模仿和存根。我慢慢地开始欣赏和理解隔离测试和模拟/存根的概念。我有一个基本的问题,我认为通过代码更容易解释这个问题:
class NewsItem < ActiveRecord::Base
...
scope :ordered, order("created_at DESC")
...
end
在我现在的模型测试中,我希望测试返回news_items有序列表的行为。通过使用FactoryGirl DB-touching测试,我实现了以下功能:
# TODO use mocking and stubbing here
it "sho
我正在使用rails,factory_girl_rails和mogoid_rspec宝石.在添加了工厂女孩创业板之后,我一直为我的用户工厂获得错误Factory not registered: user (ArgumentError)。以下是相关的代码片段:
在我的档案里:
group :development, :test do
gem 'byebug'
gem 'rspec-rails'
gem 'mongoid-rspec', '~> 2.1.0'
end
group :test do
gem
我有一个带有FactoryGirl和rSpec的新Rails 4项目。在我的spec_helper.rb中有:
# lots of stuff
RSpec.configure do |config|
# more stuff
config.include FactoryGirl::Syntax::Methods
end
我还删除了这个文件中的rspec/autorun要求。
一个简单的规范:
require 'spec_helper'
describe User do
build(:user)
end
有一个简单的工厂:
FactoryGirl.define do
最近,我将Rails 4应用程序从RSpec 2.X升级到2.99,尽管已经运行了,但我的一些测试仍然失败。
require 'spec_helper'
describe Invoice, :type => :model do
before :each do
@user = FactoryGirl.create(:user)
@invoice = FactoryGirl.create(:invoice, :user => @user)
end
it "is not open" do
expect {
我找到了一篇关于的博客文章(作者是BigBinary --它恰好是的迷你/规范版本)。
你能给我展示一下没有的spec框架的等效吗--只有Minitest (Rails)?
思维机器人方法(RSpec)
spec/factories_spec.rb
FactoryGirl.factories.map(&:name).each do |factory_name|
describe "The #{factory_name} factory" do
it 'is valid' do
build(factory_name).should
在商店模式中:
class Shop < ActiveRecord::Base
alias_attribute :token, :basic_token
# basic_token column is set NOT to be NULL in DB
end
在商店工厂:
FactoryGirl.define do
factory :shop do
basic_token '12313123123123123123123'
...
...
end
end
在RSPEC商店主计长:
require 'rails_helper&
我是RSpec的新手,所以我想在一个简单的测试上寻求一点帮助:
# controller method
def show
@group = Group.find(params[:id])
@group_members = @group.group_members.order("posts ASC")
end
# in my rspec
it "should show order correctly" do
@group = FactoryGirl.create(:group)
@user_1 = FactoryGirl.create(:user
我正在用RSPEC在rails上测试我的控制器。下面是我正在测试的控制器动作的相关部分,
控制器代码:
before do
@company=FactoryGirl.create(:company)
@customer=FactoryGirl.create(:customer, company_id: @company.id)
@job=FactoryGirl.create(:job, customer_id: @customer.id, company_id: @company.id)
end
class JobsController < Appli
我尝试用Rspec测试我的控制器:
let(:valid_attributes) {
FactoryGirl.attributes_for(:job)
}
....
describe "GET #new" do
it "assings a new job" do
get :new
expect(assigns(:job)).to be_a_new(Job)
end
end
我的工厂:
FactoryGirl.define do
facto
我正在使用let为Rspec创建一些依赖模型。
问题是我必须按照特定的顺序处理很多对象。这是我的代码,如果有更好的方法,请告诉我
let(:users) do
FactoryGirl.create_list(:users, 10)
end
let(:contacts) do
contacts = []
users.each do |user|
contacts << FactoryGirl.create_list(:contact, 3, user: user.id)
end
contacts
end
我不喜欢创建联系人的方式,我想知道是否有更好的方
我正在使用Rspec和selenium-webdriver gem来测试web应用程序。我希望在我的测试中不包括工厂,以模拟用户,而不是每次都手动创建用户。因此,我让gem安装factory_girl,在我的spec_helper中添加了required行,创建了一个工厂,并在我的规范文件中包含了一些行。在运行测试时,我得到一个错误:失败/错误: FactoryGirl.build(: User ) NameError: uninitialized constant User
这是我的spec_helper.rb
RSpec.configure do |config|
config.inc
我不明白为什么我在两个规范中的记录没有回滚:我有两个表,县has_many医院
require "rails_helper"
RSpec.describe Hospital, type: :model, focus: true do
context "created along with new hospital created" do
it "should create new hospital" do
@prefecture = FactoryGirl.create(:prefecture)
ho
在我的RSpec user_spec.rb中,我的一个FactoryGirl工厂似乎是有效的,我可以在它上调用should be_valid,这个测试将通过(如果我在工厂user_name中放置一个空字符串,那么测试就会中断)。
describe User do
it "has a valid factory" do
# should be_valid works fine
FactoryGirl.create(:user).should be_valid
end
it "is invalid without a name"
你好,我有一个非常简单的Rspec测试。
一直起作用直到我用了“工厂女孩”。我已经尝试了很多方法来让它通过。
我一直试图让它通过大约90分钟,这是一个如此简单的测试。
有人知道吗?
这是错误
1) Size is invalid without a title
Failure/Error: let(:size02) { FactoryGirl.create :size02 }
ActiveRecord::RecordInvalid:
Validation failed: Title can't be blank
这是测试
require
我正在用RSpec为Cookbook模型编写模型测试。每个用户都有has_many食谱,每个菜谱belongs_to都有一个用户。为了确保没有用户就不能创建Cookbook,我编写了以下测试:
it "is invalid without a user" do
expect(FactoryGirl.build(:cookbook, user: nil)).to be_invalid
end
但是,在运行rspec时,我得到以下输出:
1) Cookbook is invalid without a user
Failure/Error: expect(FactoryG
我正在尝试用RSpec编写一个特性测试,以测试我的符号的形式,但我总是得到以下内容:
1) the signin process signs me in
Failure/Error: visit '/users/sign_in'
ActionController::RoutingError:
No route matches [GET] "/users/sign_in"
但这条路线存在:
Prefix Verb URI Pattern
我遵循,但在引擎中构建代码。测试是在spec/controllers/concerns/handicap/authenticable_spec.rb上进行的,如下所示
require 'spec_helper'
require_relative '../../../../app/controllers/concerns/handicap/authenticable.rb'
class Authentication
include Handicap::Authenticable
end
module Handicap
describe Auth
我的大部分测试都在工作,但出于某种原因,FactoryGirl.build ...在此语句结束时,should_not be_valid不工作
describe "exceed the maximum number of subscriptions" do
@user = FactoryGirl.create(:user)
loop_count = GlobalVar::MAX_SUBSCRIPTIONS
loop_count.times do
@topic = FactoryGirl.create(:topic)
@subscription =