使用Rspec/Capybara进行RSpec测试。
假设我们有基本CRUD的PostsController。测试什么?创建新的帖子(# new ),显示所有的帖子(#索引),销毁post (#destroy)等等,如下面的代码所示,或者以另一种方式:It allows user to create new post; when posts doesnt't exist it renders 404 ...; when user is blocked render 403?
require 'spec_helper'
feature 'Post managem
我需要用PHP实现这一点:
if submit isset
AND myname isset
AND adress not empty
AND phonenumber not empty
AND email not empty
AND (
institution OR g_year not empty (one should be 'full')
OR
donation_p OR loan_p isset (should be set)
OR
donation_m OR loan_m isset (should be set)..
)
我写的内
我正在尝试测试当前路径,并检查它是否在post_path(post)上。我非常确定测试应该会通过。但我得到了got: #<Capybara::Session> (using ==)。我真的不明白这是什么。
这是测试代码
require 'spec_helper'
describe PostsController do
subject { page }
let(:first_user_is_admin) { FactoryGirl.create(:user) }
describe "Not signed in user cannot see
使用哪个匹配程序,以及如何测试是否正确分配了@post_comment和@post_comment.user?
expect(assigns(:post_comment)).to be_a_new(PostComment)不在这里工作。
更新:
通过下面的设置,我还会得到以下错误。为了能够测试无效的吸引符,我应该更改什么?
Posts::PostCommentsController when user is logged in POST create with invalid attributes doesn't save the new product in the db
Fail
我要测试一个模型,像这样;
class Post < ActiveRecord::Base
validates_presence_of :title
# ...
end
我发现了两种测试验证的主要方法:
1.使用thoughtbot公司的肩部创业板
describe Post do
it { should validate_presence_of(:title) }
end
2.直接检验验证,如书中所述。
describe Post do
it "must has title" do
post = Post.create
expect(po
我正在试着写一个功能测试。我的测试如下所示:
describe PostsController do
it "should create a Post" do
Post.should_receive(:new).once
post :create, { :post => { :caption => "ThePost", :category => "MyCategory" } }
end
end
我的PostsController (实际上是其中的一部分)如下所示:
PostController <
post_controller文件
class PostsController < ActionController::Base
before_action :authenticate_user!
def index
@post = current_user.posts.paginate(page: params[:page], per_page: 5)
respond_to do |format|
format.html
format.json { render json: @post }
end
end
def new
@post = Post.new
end
我对测试还是很陌生的,我仍然把我的头绕在“工厂女孩”周围,我相信这是这次失败的罪魁祸首。虽然解决方案可能很简单,但我已经搜索了其他带有相同失败信息的帖子,但答案并不适用于我。
我决定通过构建这个简单的博客应用程序来学习BDD/TDD。以下是失败消息:
Failures:
1) PostsController POST create creates a post
Failure/Error: expect(response).to redirect_to(post_path(post))
Expected response to be a <redirect&
我收到消息"feed.models.Post.DoesNotExist: Post matching query不存在“。我相信它是在UpdatePost类中,我不明白,因为有一个用一个id 1创建的帖子。为什么会这样呢?编辑:我添加了delete以全面测试CRUD功能
from django.test import TestCase, SimpleTestCase
from django.contrib.auth.models import User
from django.urls import reverse
from feed.models import Post
clas
我有一个模型类如下所示:
class Post < ActiveRecord::Base
has_many :comments
def tags
comments.map(&:tag).uniq.sort
end
end
根据我的理解,下面是最好的rspec:
context 'tags' do
let(:post) { Post.find_by_code('TX') }
it 'returns sorted alphabetically list of available tags' do
我正在测试post控制器,注册用户可以创建post。
it 'should allow registered user to create post' do
user = FactoryGirl.create(:user)
sign_in user
expect {
article_params = FactoryGirl.attributes_for(:post)
post :create, :article => article_params
}.to_not change(Post, :count)
Server单元测试中的测试初始化脚本在每个测试方法之前运行,测试清理在每个测试方法之后运行。
例如,我有这样的结构
UnitTests -- Main Project
- FooSchema -- Test Class
- SprocFoo1 -- Individual Unit Tests / Test Methods
- SprocFoo2
- BarSchema
- SprocBar1
- SprocBar2
测试是这样运行的
-- Test Initialiaze for TestClass FooS
我正在编写一个Django应用程序,它使用我创建的REST api。其目的是使用web应用程序来证明api用例。因此,在我看来,我使用python-request库调用api,如下所示:
def my_view_method(request):
if request.method == 'POST':
form = MyForm(request.POST)
if form.is_valid():
data = form.cleaned_data
data_to_post = {
'field
我正在用Python测试一个Trello,我有两个文件,第一个文件包含创建随机名称的代码,还有3个用于POST、GET和PUT方法的函数。
我的单元在另一个文件中:
import unittest
from board_details import *
class BoardDetails(unittest.TestCase):
# create new board, get its details and then change board's name
def test_post_board(self):
name = nonce(10)
我正在进行以下功能测试
RSpec.feature 'Show post', :type => :feature do
background do
post = create(:post)
user = create(:user)
sign_in_with user # do login
end
scenario 'can view a single post' do
visit root_path
click_link(href: post_path(post))
expect(page.c
我的Django单元中有一个TransactionManagementError。这是我的模型:
class Like(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
post = models.ForeignKey('Post', on_delete=models.CASCADE)
created = models.DateTimeField(auto_now_add=True)
class Meta:
constraints =
当我运行我的规范时,它显示了
本地跳转没有给定的块(产量)
我的服务档案
service/update_many_post.rb
class UpdateManyPost
def call
posts.each do |post|
response = UpdateAPost.new(post: post).call
yield response
end
end
private
def posts
Post.all
end
end
/service/update_a_post.rb
class Up
我需要使用unittest测试这段代码,帮助我找出它们是如何测试的。
def post(self, request, pk):
form = ReviewForm(request.POST)
movie = Movie.objects.get(id=pk)
if form.is_valid():
form = form.save(commit=False)
if request.POST.get("parent", None):
form.parent_id = int(request.POST.g
我生成了带有帖子资源的脚手架应用程序。
以下是测试代码
describe "GET index" do
it "assigns all posts as @posts" do
post = Post.create! valid_attributes
get :index, {}, valid_session
assigns(:posts).should eq([post])
end
end
下面是posts#index的实现
# GET /posts
# GET /posts.json
de
我是ruby on rails的新手。当我在comment_spec.rb上运行rspec时,我得到了一个未定义的方法错误
1) after_save calls 'Post#update_rank' after save
Failure/Error: request.env["HTTP_REFERER"] = '/'
NameError:
undefined local variable or method `request' for #<RSpec::ExampleGroups::AfterSa