我有2个模型,一个接受另一个的属性,我试图找到一个聪明的方法来使用Factory girl为两者设置数据.
Class Booking
has_many :booking_items
accepts_nested_attributes_for :booking_items
Class BookingItem
belong_to :booking
Run Code Online (Sandbox Code Playgroud)
Factory.define :booking do |f|
f.bookdate Date.today+15.days
f.association :space
f.nights 2
f.currency "EUR"
f.booking_item_attributes Factory.build(:booking_item) # doesn't work
end
Factory.define :booking_item do |f|
f.association :room
f.bookdate Date.today
f.people 2
f.total_price 20
f.association :booking
end
Run Code Online (Sandbox Code Playgroud)
require "spec_helper"
describe Booking do
before(:each) do
@booking = Factory.create(:booking)
end
it "should be valid" do
#needs children to be valid
@booking.should be_valid
end
end
Run Code Online (Sandbox Code Playgroud)
我环顾了rdocs,但似乎无法找到我想要的东西.
我不能让Capybara成功地工作,它抱怨这has_text是一种未定义的方法.
我创建了一个新的rails 3.1项目(rails new test -T).
的Gemfile:
source 'http://rubygems.org'
gem 'rails', '3.1.3'
gem 'sqlite3'
group :assets do
gem 'sass-rails', '~> 3.1.5'
gem 'coffee-rails', '~> 3.1.1'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
group :test do
gem 'rspec-rails'
gem 'capybara'
end
Run Code Online (Sandbox Code Playgroud)
我已经安装规范文件夹:rails g rspec:install.
投机/ spec_helper.rb:
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
config.mock_with :rspec
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = …Run Code Online (Sandbox Code Playgroud) 我得到了一个失败的测试,我调试很困难.
我的测试用例是:
it "routes to #active" do
get ("/parties/active").should route_to("parties#active")
end
Run Code Online (Sandbox Code Playgroud)
我的路线是:
resources :parties do
get 'active', :on => :collection
end
Run Code Online (Sandbox Code Playgroud)
当我运行测试用例时,我收到以下错误:
NoMethodError: undefined method `values' for "/parties/active":String
Run Code Online (Sandbox Code Playgroud)
完整错误输出:https://gist.github.com/1748264
当我运行rake路线时,我看到:
active_parties GET /parties/active(.:format) parties#active
parties GET /parties(.:format) parties#index
POST /parties(.:format) parties#create
new_party GET /parties/new(.:format) parties#new
edit_party GET /parties/:id/edit(.:format) parties#edit
party GET /parties/:id(.:format) parties#show
PUT /parties/:id(.:format) parties#update
DELETE /parties/:id(.:format) parties#destroy
root / parties#show
Run Code Online (Sandbox Code Playgroud)
我正在运行的测试几乎与脚手架生成的测试相同:
it "routes to #new" do
get("/parties/new").should route_to("parties#new")
end
Run Code Online (Sandbox Code Playgroud)
知道发生了什么事吗?
我已经为我的用户模型编写了一组23个测试,并且所有测试都失败并出现以下错误:
Failure/Error: Unable to find matching line from backtrace
ArgumentError:
block not supplied
Run Code Online (Sandbox Code Playgroud)
我曾尝试搞乱不同版本的水豚,rspec-rails和咆哮,但说实话,我真的不知道我在这里做什么,并没有真正理解错误.我真的很感激任何能指出我正确方向的人的帮助.
这是我user_spec.rb文件中的一个示例
require 'spec_helper'
describe User do
before @user = User.new(name: 'example', password: 'foobar', password_confirmation: 'foobar',
email: 'example@example.com', description: 'Lorem ipsum dolor...')
subject { @user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
it { should respond_to(:password_digest) }
it { should respond_to(:description) }
it { should respond_to(:admin) }
it { should respond_to(:remember_token) …Run Code Online (Sandbox Code Playgroud) Failure/Error: post :create, Devise.token_authentication_key => @foo.authentication_token, receipt_image: "foo.png"
Paperclip::AdapterRegistry::NoHandlerError: No handler found for "foo.png"
Run Code Online (Sandbox Code Playgroud)
如何在rspec代码中传递图像,我也尝试使用curl命令的@ foo.png.
RSpec提供了参数regex匹配器,如下例所示:
mock_smtp.should_receive(:send_message).with(anything, anything, /@just-testing.com/)
Run Code Online (Sandbox Code Playgroud)
这很好,并允许我捕获与正则表达式匹配的参数,这适用于示例(匹配字符串,以'@ just-testing.com'结尾).
我的问题是我不能让这个适用于这种类型的参数:
msgstr = <<END_OF_MESSAGE
From: me <#{from_email}>
To: #{to_name} <#{to_email}>
Subject: #{subject}
MIME-Version: 1.0
Content-type: text/html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">...</html>
END_OF_MESSAGE
Run Code Online (Sandbox Code Playgroud)
使用正则表达式可以捕获msgstr变量中的任何内容.
如何测试msgstr变量中的值?
我最近将我的Rails 4应用程序从RSpec 2.X升级到2.99,尽管已经运行了Transpec,但我的一些测试仍然失败.
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 {
FactoryGirl.create(:payment, :invoice => @invoice, :amount => 100)
}.to change{@invoice.reload.open?}.from(true).to(false)
end
it "is open" do
expect {
FactoryGirl.create(:payment, :invoice => @invoice, :amount => 99.99)
}.to_not change{@invoice.reload.open?}.to(false)
end
end
Run Code Online (Sandbox Code Playgroud)
第一次测试就像RSpec升级之前一样.
但是,第二个测试会抛出错误:
Failure/Error: expect {
`expect { }.not_to change { }.to()` is deprecated.
Run Code Online (Sandbox Code Playgroud)
我必须将语法更改为什么?
我已经尝试了几件事not_to,be_falsey等等.到目前为止,没有任何工作.
谢谢你的帮助.
我继承了许多没有真正工作的FactoryGirl工厂,我试图把它们搞得很好.部分原因是使用FactoryGirl.lint.然而,到目前为止,我已经能够找到哪些工厂失败了,对于任何一个工厂来说,它都能运行
x = FactoryGirl.build :invalid_factory
x.valid? # returns false as expected
x.errors # prints out the validation errors for that object
Run Code Online (Sandbox Code Playgroud)
我想做的是避免为每个工厂做这件事.有没有办法快速FactoryGirl.lint写出每个无效工厂的错误?要传递的标志,要设置的参数?文档非常稀少.lint
使用rails 4.2.0和最新版本的RSpec,我生成了控制器测试。
如何确保我的管理员用户已登录?
例如:如果current_user.admin?
在rspec测试中,它像这样提到它:
let(:valid_session) { {} }
Run Code Online (Sandbox Code Playgroud)
如何输入有效的会话?
所以我在看:https://rubyplus.com/articles/1491-Basic-TDD-in-Rails-Writing-Validation-Tests-for-the-Model
只是看到测试技术,我看到了这个:
require 'rails_helper'
describe Article, type: :model do
it 'is valid if title and description fields have value' do
expect do
article = Article.new(title: 'test', description: 'test')
article.save
end.to change{Article.count}.by(1)
end
end
Run Code Online (Sandbox Code Playgroud)
特别是最后一行:end.to change{Article.count}.by(1).阅读https://relishapp.com/rspec/rspec-expectations/v/3-7/docs/built-in-matchers/change-matcher
它特别说:
更改匹配器用于指定代码块更改某些可变状态.您可以使用以下两种形式之一指定更改内容:
这是有道理的.但是Article.count在代码块中进行测试实际上并没有"做"任何事情(这article.save实际上是什么改变了,Article.count所以这究竟是如何工作的?测试是否会在代码块运行之前看看它们是什么?"prerun "它...比较.by(1)后?
谢谢
rspec-rails ×10
rspec ×6
ruby ×3
factory-bot ×2
capybara ×1
devise ×1
paperclip ×1
regex ×1
rspec3 ×1
unit-testing ×1